<p>I am running a Docker image using Kubernetes. I would like to pass to the container the digest of the image being used. So that the code inside the container can use this for debugging/logging. The problem is that I do not seem to be able to find a way to do this without hard-coding the image digest into the pod configuration.</p>

<p>Is there a way to define pod configuration way so that it dynamically passes the digest as environment variable for whichever version of Docker image it ends up using?</p>

<p>Whatever Kubernetes happens to know can be injected using the <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">downward API</a>.  That set of data is in <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/#pod-v1-core" rel="nofollow noreferrer">the API reference for Pod objects</a>.</p>

<p>It looks like this should work:</p>

<pre><code>env:
  - name: DOCKER_IMAGE_ID
    valueFrom:
      fieldRef:
        fieldPath: status.containerStatuses[0].imageID
</code></pre>

<p>You may prefer to inject the <code>spec.containers[0].image</code> name, which will be easier to understand after the fact.  If you're using a tool like <a href="https://helm.sh/" rel="nofollow noreferrer">Helm</a> to generate the configuration, you can also use its values system:</p>

<pre><code>image: {{ .Values.image }}:{{ .Values.tag }}
env:
  - name: DOCKER_IMAGE_TAG
    value: {{ .Values.tag }}
</code></pre>
