I am running a Python application inside of a python:3.11.2-alpine
container.
The Python application needs information about the container name within the running container.
I have read about how to get the general container information
Mostly the answers are how to get the container id (e.g. by environment variable HOSTNAME
).
But there is also the need for the specific container name (see the last column in the following code block).
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3b7ef8f1d979 myfancyimage:0.0.9 "/bin/sh..." 22 hours ago Up 22 hours (unhealthy) myfancyname
I have also checked the additional environment variables inside the container, but I have not found any information about the container name.
Is there a way to get the container name?
2
Answers
In my running Docker instance, running
hostname
does give the container name, not the container ID. In my case the name has the short-form ID appended to it for convenience, so it has the form<my_container_name>-<shortID>
.Could you share a code snippet showing how you are getting the ID and container names?
https://docs.docker.com/engine/reference/run/#container-identification
The
--name
option sets a custom name to identify your container on the Docker host, the-h, --hostname
option sets the container’s hostname.You could set both options to get the information from the
$HOSTNAME
environment variable inside your container: