How to get the image name of a docker container from inside the container

This type of information/metadata is not passed into the container by default. There are 2 reasons for this.

  1. if every metadata about the container was passed into the container, it could potentially pose a security risk when someone breaks into it
  2. container shouldn’t need this kind of information anyway

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.

Leave a Comment