<p>When I SSH onto a <strong>running</strong> Docker container in a Kubernetes cluster and run <code>os.getenv(&quot;HOSTNAME&quot;)</code> from within a python interpreter, I am able to see the name of the <strong>deployment</strong> being used.</p>
<p>But if I try and run <code>os.getenv(&quot;HOSTNAME&quot;)</code> in a script that gets run from the <code>Dockerfile</code>, the env-var is <code>null</code>.</p>
<p>Is this expected? Is there some workaround here?</p>
<p><strong>UPDATE</strong>: I tried to get the contents from <code>/etc/hostname</code> instead and to my surprise I got <code>debuerreotype</code>. After some googling I saw that that is the base Debian image in use and apparently <a href="https://github.com/debuerreotype/debuerreotype/blob/master/docker-run.sh#L50" rel="nofollow noreferrer">it passes that name as the hostname</a></p>
<ul>
<li>Opened an <a href="https://github.com/debuerreotype/debuerreotype/issues/160" rel="nofollow noreferrer">Issue</a> with them in the meantime</li>
<li>(I still don't understand why I get the correct value when I SSH into the container though)</li>
</ul>

Felipe
<p>The problem was that I was running scripts in the <code>RUN</code> step of the <code>Dockerfile</code>.</p>
<p>This means that this code runs at <strong>image build time</strong>, so whatever Environment variables I was able to retrieve were those of the build-time environment.</p>
<p>I was able to correctly retrieve the env-vars when I did it inside the code that gets run at <strong>container run time</strong>, i.e. in the <code>ENTRYPOINT</code> command.</p>
