I would like to make my docker containers
aware of their configuration, the same way you can get information about EC2 instances through metadata.
I can use (provided docker
is 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 at least the get the full ID of the container, because HOSTNAME
is actually shortened to 12 characters and docker seems to perform a "best match" on it.
Also, how can I get the external IP of the docker host (other than accessing the EC2 metadata, which is specific to AWS)
You can use this command line to identify the current container ID (tested with docker 1.9).
Then, a little request to Docker API (you can share /var/run/docker.sock) to retrieve all informations.
Some posted solutions have stopped working due to changes in the format of
/proc/self/cgroup
. Here is a single GNU grep command that should be a bit more robust to format changes:For reference, here are snippits of /proc/self/cgroup from inside docker containers that have been tested with this command:
Linux 4.4:
Linux 4.8 - 4.13:
You can communicate with docker from inside of a container using unix socket via Docker Remote API:
https://docs.docker.com/engine/reference/api/docker_remote_api/
In a container, you can find out a shortedned docker id by examining
$HOSTNAME
env var. According to doc, there is a small chance of collision, I think that for small number of container, you do not have to worry about it. I don't know how to get full id directly.You can inspect container similar way as outlined in banyan answer:
Response:
Alternatively, you can transfer docker id to the container in a file. The file is located on "mounted volume" so it is transfered to container:
The docker id (shortened) will be in file /mydir/host1.txt in the container.
As an aside, if you have the pid of the container and want to get the docker id of that container, a good way is to use nsenter in combination 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/"
Unless overridden, the hostname seems to be the short container id in Docker 1.12
Externally
Use
docker inspect
.Can get ip as follows.