Pass balena hostname to container

I want a container to have an environment variable with the host’s hostname (AKA the uuid) as the value. Unfortunately, hostname is not an environment variable. How might I accomplish this?

I’m really hoping there’s a way to do this from the docker-compose.yml, since I want to avoid running commands on the host OS.

Hey @lucas.bodnyk welcome to the forums.

You’re totally right to want to avoid running commands on the host OS! A couple of options here: Your container should already have by default the environment variable BALENA_DEVICE_UUID, of which the first 6 characters is the default hostname.

Secondly, given that it’s possible that the hostname can be changed and may not be static, you could take a look at using the Supervisor API to request the current hostname. In order to use this you have to add a label io.balena.features.supervisor-api to the container, and then you can use the GET /v1/device/host-config endpoint to obtain the hostname within your container and set it as an environment variable. Note that there are also methods to change the hostname too, if required.

@chrisys thanks, I didn’t realize that BALENA_DEVICE_UUID already existed–this was exactly what we needed!