<p>Say I have the following pod spec. </p>

<pre><code>apiVersion: apps/v1beta1
kind: Deployment
metadata:
  # Unique key of the Deployment instance
  name: deployment-example
spec:
  # 3 Pods should exist at all times.
  replicas: 3
  template:
    metadata:
      labels:
        # Apply this label to pods and default
        # the Deployment label selector to this value
        app: nginx
    spec:
      containers:
      - name: nginx
        # Run this image
        image: nginx:1.10
</code></pre>

<p>Here, the name of the container is <code>nginx</code>. Is there a way to get the "nginx" string from within the running container?</p>

<p>I mean, once I exec into the container with something like </p>

<pre><code>kubectl exec -it &lt;pod-name&gt; -c nginx bash
</code></pre>

<p>Is there a programmatic way to get to the given container name in the pod spec ? </p>

<hr>

<p>Note that this is not necessarily the docker container name that gets printed in <code>docker ps</code>. Kubernetes composes a longer name for the spawned docker container. </p>

<hr>

<p>The <a href="https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#capabilities-of-the-downward-api" rel="noreferrer">downward api</a> looks promising in this regard. However <code>container name</code> is not mentioned in the <code>Capabilities of the Downward API</code> section. </p>

<p>The container name is not available trough the downward api. You can use <a href="http://www.yaml.org/spec/1.2/spec.html#alias//" rel="noreferrer">yaml anchors and aliases (references)</a>. Unfortunately they are not scoped so you will have to come up with unique names for the anchors - it does not matter what they are as they are <em>not</em> present in the parsed document.</p>
<blockquote>
<p>Subsequent occurrences of a previously serialized node are presented as alias nodes. The first occurrence of the node must be marked by an anchor to allow subsequent occurrences to be presented as alias nodes.</p>
<p>An alias node is denoted by the “*” indicator. The <strong>alias refers to the most recent preceding node having the same anchor</strong>. It is an error for an alias node to use an anchor that does not previously occur in the document. It is not an error to specify an anchor that is not used by any alias node.</p>
<pre><code>First occurrence: &amp;anchor Foo
Second occurrence: *anchor
Override anchor: &amp;anchor Bar
Reuse anchor: *anchor
</code></pre>
</blockquote>
<p>Here is a full working example:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: reftest
spec:
  containers:
  - name: &amp;container1name first
    image: nginx:1.10
    env:
    - name: MY_CONTAINER_NAME
      value: *container1name
    - name: MY_POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
  - name: &amp;container2name second
    image: nginx:1.10
    env:
    - name: MY_CONTAINER_NAME
      value: *container2name
    - name: MY_POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
</code></pre>
