<p>I want to display the pod IP address in an nginx pod. Currently I am using an init container to initialize the pod by writing to a volume.</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: init-demo
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: workdir
      mountPath: /usr/share/nginx/html
  # These containers are run during pod initialization
  initContainers:
  - name: install
    image: busybox:1.28
    env:
    - name: POD_IP
      valueFrom:
        fieldRef:
          fieldPath: status.podIP
    command:
    - echo 
    - $(POD_IP) &gt;&gt; /work-dir/index.html
    volumeMounts:
    - name: workdir
      mountPath: &quot;/work-dir&quot;
  dnsPolicy: Default
  volumes:
  - name: workdir
    emptyDir: {}
</code></pre>
<p>This should work in theory, but the file redirect doesn't work and the mounted file in the nginx container is blank.  There's probably an easier way to do this, but I'm curious why this doesn't work.</p>

wootscootinboogie
<p>Nothing is changed, except how <code>command</code> is passed in the init container. See <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#run-a-command-in-a-shell" rel="nofollow noreferrer">this</a> for an explanation.</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: init-demo
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: workdir
      mountPath: /usr/share/nginx/html
  # These containers are run during pod initialization
  initContainers:
  - name: install
    image: busybox:1.28
    env:
    - name: POD_IP
      valueFrom:
        fieldRef:
          fieldPath: status.podIP
    command: 
    - 'sh'
    - '-c'
    - 'echo $(POD_IP) &gt; /work-dir/index.html'
    volumeMounts:
    - name: workdir
      mountPath: &quot;/work-dir&quot;
  dnsPolicy: Default
  volumes:
  - name: workdir
    emptyDir: {}
</code></pre>
