Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

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.

like image 923
Donna Schweitzer Avatar asked Aug 11 '20 15:08

Donna Schweitzer


1 Answers

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.

like image 96
Matus Dubrava Avatar answered Sep 30 '22 18:09

Matus Dubrava