0

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)
Felipe
  • 11,557
  • 7
  • 56
  • 103
  • It's a question of the shell and OS the container is using. Some shells use $HOST (tcsh) others (bash) use $HOSTNAME. – Martlark Aug 27 '23 at 23:04
  • The hostname doesn’t matter much for a K8S POD, so, it’s not clear to me how you intend to use the value. If you want to know the POD ID, then you can use the Downward API. See [this](https://stackoverflow.com/q/49582349/839733). – Abhijit Sarkar Aug 27 '23 at 23:17
  • @AbhijitSarkar I want to display in the screen which pod is serving that request, for debugging purposes (it's a web application) – Felipe Aug 27 '23 at 23:19
  • @Martlark just tested it -- didn't work unfortunately. Still `null` – Felipe Aug 27 '23 at 23:21
  • add `export HOSTNAME=$(hostname)` to your entrypoint script – Martlark Aug 27 '23 at 23:39
  • i don't have access to the caller script =/ – Felipe Aug 28 '23 at 00:30
  • @Felipe PODs come and go, so, a hostname printed in the logs means absolutely nothing for debugging because that host may no longer be around when the debugging is required. Not to mention that developers usually don't have access to the hostname anyway, so seeing that host abc served a particular user request isn't exactly mind-blowing useful. – Abhijit Sarkar Aug 28 '23 at 01:40
  • Does [How can I use Python to get the system hostname?](https://stackoverflow.com/questions/4271740/how-can-i-use-python-to-get-the-system-hostname) answer your question? If you do actually need the hostname for whatever reason, `socket.hostname()` is probably the right way to get it. – David Maze Aug 28 '23 at 01:46
  • @DavidMaze `socket.hostname()` returns a default value (`buildkitsandbox`) when I call it during container startup. And it returns the pod ID when I call with from within a running Container. The problem remains -- different results depending on whether I call it during container startup _or_ from a a running container. – Felipe Aug 28 '23 at 18:38

1 Answers1

0

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.

Felipe
  • 11,557
  • 7
  • 56
  • 103