Docker, how to get container information from a container?

I would like to tell my docker containers about my configuration in the same way that you can get information about EC2 instances through metadata.

I can use (assuming docker listening on port 4243 )

 curl http://172.17.42.1:4243/containers/$HOSTNAME/json 

to get some of its data, but would like to know if there is a better way to at least get the full container identifier, because HOSTNAME is actually shortened to 12 characters, and the docker seems to be performing the โ€œbest matchโ€ on it.

Also, how can I get the external IP address of the docker host (except for access to EC2 metadata, which is related to AWS)

+109
linux docker
Jan 08 '14 at 12:13
source share
15 answers

I found that the container identifier can be found in / proc / self / cgroup

So you can get the id with:

 cat /proc/self/cgroup | grep -o -e "docker-.*.scope" | head -n 1 | sed "s/docker-\(.*\).scope/\\1/" 
+65
Sep 08 '14 at 17:14
source share

If not overridden, the hostname seems to be a short container identifier in Docker 1.12

 root@d2258e6dec11:/project# cat /etc/hostname d2258e6dec11 

Externally

 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d2258e6dec11 300518d26271 "bash" 5 minutes ago $ docker -v Docker version 1.12.0, build 8eab29e, experimental 
+50
Aug 16 '16 at 20:27
source share

You can communicate with docker from inside the container using a unix socket via the Docker Remote API:

https://docs.docker.com/engine/reference/api/docker_remote_api/

In the container, you can find out the shortedned docker code by looking at $HOSTNAME env var. According to the document, there is a small chance of a collision, I think that for a small number of containers you have nothing to worry about. I do not know how to get the full identifier directly.

You can check the container in the same way as stated in banyan's answer:

 GET /containers/4abbef615af7/json HTTP/1.1 

Answer:

 HTTP/1.1 200 OK Content-Type: application/json { "Id": "4abbef615af7...... ", "Created": "2013.....", ... } 

Alternatively, you can pass the docker id to a container in the file. The file is on the "mounted volume", so it is transferred to the container:

 docker run -t -i -cidfile /mydir/host1.txt -v /mydir:/mydir ubuntu /bin/bash 

The docker identifier (abbreviated) will be in the file / mydir / host 1.txt in the container.

+33
Jan 08 '14 at 16:15
source share

This will result in the full identifier of the container from the container:

 cat /proc/self/cgroup | grep "cpu:/" | sed 's/\([0-9]\):cpu:\/docker\///g' 
+21
Feb 12 '15 at 16:57
source share

WARNING: You must understand the security risks of this method before considering it. John Risk Summary:

By giving the container access to /var/run/docker.sock , it is [trivially easy] to break out of /var/run/docker.sock provided by /var/run/docker.sock and gain access to the host machine. Obviously, this is potentially dangerous.




Inside the container, dockerId is your hostname. So you could:

  • install docker-io package in your container with the same version as the host
  • run it with --volume/var/run/docker.sock: /var/run/docker.sock --privileged
  • finally run: docker inspect $(hostname) inside the container

Avoid this. Do this only if you understand the risks and have a clear risk reduction.

+19
Feb 27 '14 at 19:11
source share

To make it simple,

  • Container ID is the name of your host inside docker
  • Container information is available inside / proc / self / cgroup

To get the host name,

 hostname 

or

 uname -n 

or

 cat /etc/host 

The output can be redirected to any file and read from the application. For example: # hostname > /usr/src//hostname.txt

+11
Nov 13 '17 at 20:10
source share

Madaddy 's comment looks most elegant to me:

 CID=$(basename $(cat /proc/1/cpuset)) 
+10
Oct 25 '18 at 11:29
source share

I found that in 17.09 there is the easiest way to do this in the docker container:

 $ cat /proc/self/cgroup | head -n 1 | cut -d '/' -f3 4de1c09d3f1979147cd5672571b69abec03d606afcc7bdc54ddb2b69dec3861c 

Or, as already mentioned, a shorter version with

 $ cat /etc/hostname 4de1c09d3f19 

Or simply:

 $ hostname 4de1c09d3f19 
+9
Oct 05 '17 at 13:30
source share

Docker sets the default hostname of the container ID, but users can override this with --hostname . Check /proc instead:

 $ more /proc/self/cgroup 14:name=systemd:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 13:pids:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 12:hugetlb:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 11:net_prio:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 10:perf_event:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 9:net_cls:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 8:freezer:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 7:devices:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 6:memory:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 5:blkio:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 4:cpuacct:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 3:cpu:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 2:cpuset:/docker/7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 1:name=openrc:/docker 

Here is a handy single line file for retrieving the container ID:

 $ grep "memory:/" < /proc/self/cgroup | sed 's|.*/||' 7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 
+6
Nov 29 '17 at 16:17
source share

You can use this command line to identify the current container identifier (verified using docker 1.9).

 awk -F"-|/." '/1:/ {print $3}' /proc/self/cgroup 

Then, a small request to the Docker API (you can share /var/run/docker.sock) to get all the information.

+2
Jan 28 '16 at 8:05
source share
 awk -F'[:/]' '(($4 == "docker") && (lastId != $NF)) { lastId = $NF; print $NF; }' /proc/self/cgroup 
+1
Oct 08 '15 at 13:59 on
source share

Some published solutions have stopped working due to changes in the /proc/self/cgroup . Here is one GNU grep command that should be slightly more resilient to format changes:

 grep -o -P -m1 'docker.*\K[0-9a-f]{64,}' /proc/self/cgroup 

For reference, here are snippets of / proc / self / cgroup from docker containers that have been tested with this command:

Linux 4.4:

 11:pids:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope ... 1:name=systemd:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope 

Linux 4.8 - 4.13:

 11:hugetlb:/docker/afe96d48db6d2c19585572f986fc310c92421a3dac28310e847566fb82166013 ... 1:name=systemd:/docker/afe96d48db6d2c19585572f986fc310c92421a3dac28310e847566fb82166013 
+1
Aug 29 '18 at 16:15
source share

Aside, if you have a container pid and want to get the docker ID of that container, a good way is to use nsenter in conjunction with the sed magic above:

nsenter -n -m -t pid -- cat /proc/1/cgroup | grep -o -e "docker-.*.scope" | head -n 1 | sed "s/docker-\(.*\).scope/\\1/"

0
Apr 14 '16 at 14:27
source share

The easiest way I've found is: docker inspect --format = "{{. Id}}"

-four
May 17 '19 at 14:10
source share

Use docker inspect .

 $ docker ps # get conteiner id $ docker inspect 4abbef615af7 [{ "ID": "4abbef615af780f24991ccdca946cd50d2422e75f53fb15f578e14167c365989", "Created": "2014-01-08T07:13:32.765612597Z", "Path": "/bin/bash", "Args": [ "-c", "/start web" ], "Config": { "Hostname": "4abbef615af7", ... 

You can get ip as follows.

 $ docker inspect -format="{{ .NetworkSettings.IPAddress }}" 2a5624c52119 172.17.0.24 
-eighteen
Jan 08 '14 at 1:47
source share



All Articles