<p>I need to inject container port from an environment variable inside my pod. How to do that? </p>

<p>Have been through the documentation, Links:- 
1. <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/</a>
2. <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/</a></p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: default
spec:
  containers:
    - name: nginx-container
      image: nginx
      ports:
        - containerPort: $(MY_CONTAINER_PORT)
      env:
        - name: MY_CONTAINER_PORT
          value: 80
</code></pre>

<pre><code>error: error validating "nginx-pod-exposed-through-env.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 these errors, turn validation off with --validate=false
</code></pre>

<p>A way to accomplish this would be to use a templating tool such as <a href="https://get-ytt.io/" rel="nofollow noreferrer">ytt</a>. With ytt you would turn your manifest into a template like:</p>
<pre><code>#@ load(&quot;@ytt:data&quot;, &quot;data&quot;)
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: default
spec:
  containers:
    - name: nginx-container
      image: nginx
      ports:
        - containerPort: #@ data.values.port
</code></pre>
<p>And then supply a <code>values.yml</code> like:</p>
<pre><code>#@data/values
---
port: 8080
</code></pre>
<p>Assuming the original template is named <code>test.yml</code> we could run <code>ytt</code> like so to generate the output:</p>
<pre><code>$ ytt -f test.yml -f values.yml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: default
spec:
  containers:
  - name: nginx-container
    image: nginx
    ports:
    - containerPort: 8080
</code></pre>
<p>The ytt utility then lets us override the data values one the command line with <code>--data-value</code> (or <code>-v</code> for short). An example changing to port 80:</p>
<pre><code>$ ytt -v port=80 -f test.yml -f values.yml            
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: default
spec:
  containers:
  - name: nginx-container
    image: nginx
    ports:
    - containerPort: 80
</code></pre>
<p>Your original question sounded like you wanted to use environment variables. This is supported with <code>--data-values-env</code>. An example using prefix <code>MYVAL</code>:</p>
<pre><code>$ export MYVAL_port=9000
$ ytt --data-values-env MYVAL -f test.yml -f values.yml   
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: default
spec:
  containers:
  - name: nginx-container
    image: nginx
    ports:
    - containerPort: 9000
</code></pre>
<p>You can then combine <code>ytt</code> and <code>kubectl</code> to create and apply resources:</p>
<pre><code>ytt --data-values-env MYVAL -f test.yml -f values.yml | kubectl apply -f-
</code></pre>
<p>Additional information on injecting data into ytt templates is at <a href="https://github.com/k14s/ytt/blob/develop/docs/ytt-data-values.md" rel="nofollow noreferrer">https://github.com/k14s/ytt/blob/develop/docs/ytt-data-values.md</a>.</p>
