Search code examples
pythondockerkubernetesenvironment-variablesdebian

HOSTNAME env-var returns empty during Docker pod startup


When I SSH onto a running Docker container in a Kubernetes cluster and run os.getenv("HOSTNAME") from within a python interpreter, I am able to see the name of the deployment being used.

But if I try and run os.getenv("HOSTNAME") in a script that gets run from the Dockerfile, the env-var is null.

Is this expected? Is there some workaround here?

UPDATE: I tried to get the contents from /etc/hostname instead and to my surprise I got debuerreotype. After some googling I saw that that is the base Debian image in use and apparently it passes that name as the hostname

  • Opened an Issue with them in the meantime
  • (I still don't understand why I get the correct value when I SSH into the container though)

Solution

  • The problem was that I was running scripts in the RUN step of the Dockerfile.

    This means that this code runs at image build time, so whatever Environment variables I was able to retrieve were those of the build-time environment.

    I was able to correctly retrieve the env-vars when I did it inside the code that gets run at container run time, i.e. in the ENTRYPOINT command.