I need to get the name of the docker container that I am currently running, from within that container. The command that I use to run the container is
docker exec -it 9d05bea23030 bash
I am able to retrieve the ID of the container by typing
cat /etc/hostname
but I don't know how to get the name of the image. I am referring to the name that is shown under IMAGE when I type docker ps outside of the container. I need that specific name, as it contains crucial information.
This type of information/metadata is not passed into the container by default. There are 2 reasons for this.
The reason why you can get access to container's ID from within the container is simply because it is used as the container's hostname by default. If you would specify hostname when running the container with container run --hostname ...
, you wouldn't have access to the ID either.
I don't know why you need this information but the only way to get it when you are inside of the container is to first pass it to the container in one way or another. For example via an environment variable.
If you don't have access to this information from the outside of the container (which seems strange if you are able to use docker exec
), you will not get it from the running container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With