For a project, I absolutely need to know a container’s full id from inside of itself (without executing any command on the host). Is there any way to get this information?
For information, this is the function which I use from the host to get this information, which is obviously unusable from inside of a container.
I have been using the /proc/self/cgroup file to get the container id.
But when I upgraded to Docker 4.3.1, that file only contains: 0::/
$ cat /proc/self/cgroup
0::/
My program was working correctly with Docker Desktop 4.2.0, but with 4.3.1 it is not.
Is this a bug in 4.3.1 or do I need to change how I get the container id ?
When I add ‘–cgroupns=host’, to my docker command then the /proc/self/cgroup file has what I need.
Since that is not the case by default, is there some other (more reliable) way to get the ID from inside the container ?
And knowing that “If you don’t change the hostname, then the default hostname is the short version of the container id.” is very helpful.
Sometimes I have a tiny lag between when a file is written to a mounted directory inside/outside the container and when I can read it from the other side. I worry that (some of the time) my program inside the container would look for the file before it could see it, because of this delay.
docker run --rm -it \
-v /var/run/docker.sock:/var/run/docker.sock \
curltest /bin/sh
# And then inside the container:
echo $HOSTNAME
# 9e17ce753e96
curl --unix-socket /var/run/docker.sock http:/v1.38/containers/${HOSTNAME}/json
# {"Id":"9e17ce753e9677b8b255ccaea1c0bd0d8abf8f3ee5e909b4957e628ebbb16c09",
# ...
This has the downsides of:
needing to map in the docker sock,
the container must have curl,
you still have to extract it from the json string.
If the value of $HOSTNAME is set with --hostname then this does not work.
An alternative to using $HOSTNAME is to assign the container a name. I haven’t found a way to access that value internally, but I can pass it as an environment variable.
DNAME=$(date +%Y_%m_%d_%H_%M_%S)
docker run --rm -it \
-v /var/run/docker.sock:/var/run/docker.sock \
--name $DNAME -e DNAME=$DNAME \
curltest /bin/sh
# And then inside the container:
curl --unix-socket /var/run/docker.sock http:/v1.38/containers/${DNAME}/json
Without any id, I can get the info for all docker containers; so IF you have a way to figure out which container is the right one, then you don’t even need the hostname.
It seems you know how the docker API works so you could create a web service which could return the ID based on from which IP address (on the docker network) you send the request. You don’t need curl for this on an HTTP port. Bash has a builtin feature to send HTTP requests.