<p>I am setting port value in an environment property while generating Pod yaml.</p>

<pre><code>master $ kubectl run nginx --image=nginx --restart=Never --env=MY_PORT=8080 --dry-run -o yaml  &gt; Pod.yaml
</code></pre>

<p>I am trying to use the environment property MY_PORT in the ports section of my Pod yaml.</p>

<pre><code>spec:
     containers:
     - env:
       - name: MY_PORT
         value: "8080"
       image: nginx
       name: nginx
       ports:
       - containerPort: $(MY_PORT)
</code></pre>

<p>When i try to create the Pod i am getting following error message.</p>

<pre><code>error: error validating "Pod.yaml": error validating data: ValidationError(Pod.spec.containers[0].ports[0].containerPort): invalid type for io.k8s.api.core.v1.ContainerPort.containerPort: got "string", expected "integer"; if you choose to ignore theseerrors, turn validation off with --validate=false
</code></pre>

<p>I tried referencing like ${MY_PORT} , MY_PORT etc.. but all the time same error as above.</p>

<p>How i can use an environment variable value in an integer field.</p>

user1660472
<p>You can't use an environment variable there.  In the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#containerport-v1-core" rel="nofollow noreferrer"><code>ContainerPort</code> API object</a> the <code>containerPort</code> field is specified as an integer.  Variable substitution is only support in a couple of places, and where it does it is called out; see for example <code>args</code> and <code>command</code> in the higher-level <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#container-v1-core" rel="nofollow noreferrer"><code>Container</code> API object</a>.</p>

<p>There's no reason to make this configurable.  In a Kubernetes environment the pod will have its own IP address, so there's no risk of conflict; if you want to use a different port number to connect, you can set up a service where <em>e.g.</em> port 80 on the service forwards to port 8080 in the pod.  (In plain Docker, you can do a similar thing with a <code>docker run -p 80:8080</code> option: you can always pick the external port even if the port number inside the container is fixed.)  I'd delete the environment variable setting.</p>
