How to get docker container ID from within the container with cgroup v2
The --cgroupns host
fix is effective, but not available if you don't control the container's creation. Further, this docker run
option is not available in the API or docker compose (https://github.com/compose-spec/compose-spec/issues/148).
But... good news - the container ID is still exposed via /proc/self/mountinfo:
678 655 254:1 /docker/containers/7a0144cee1256c539fab790199527b7051aff1b603ebcf7ed3fd436440ef3b3a/resolv.conf /etc/resolv.conf rw,relatime - ext4 /dev/vda1 rw
679 655 254:1 /docker/containers/7a0144cee1256c539fab790199527b7051aff1b603ebcf7ed3fd436440ef3b3a/hostname /etc/hostname rw,relatime - ext4 /dev/vda1 rw
680 655 254:1 /docker/containers/7a0144cee1256c539fab790199527b7051aff1b603ebcf7ed3fd436440ef3b3a/hosts /etc/hosts rw,relatime - ext4 /dev/vda1 rw
Here's a Python snippet that'll parse it:
with open( '/proc/self/mountinfo' ) as file:
line = file.readline().strip()
while line:
if '/docker/containers/' in line:
containerID = line.split('/docker/containers/')[-1] # Take only text to the right
containerID = containerID.split('/')[0] # Take only text to the left
break
line = file.readline().strip()
Credit goes to richgriswold: https://community.toradex.com/t/python-nullresource-error-when-running-torizoncore-builder-build/15240/4
How can I get Docker Linux container information from within the container itself?
I've found out that the container id 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/"
CoreOS - get docker container name by PID?
Something like this?
$ docker ps -q | xargs docker inspect --format '{{.State.Pid}}, {{.ID}}' | grep "^${PID},"
[EDIT]
Disclaimer This is for "normal" linux. I don't know anything useful about CoreOS, so this may or may not work there.
Docker jenkins ssh-agent not running inside container on Ubuntu 22.04
It turned out that the problem was related to cgroup v2 after all. It seems that when using v2 the cgroup namespace is private by default when you create a container, in my case the Jenkins agents, which caused the container id to not be available in /proc/self/cgroup
.
The easy solution is to run the docker container with --cgroupns host
as suggested in another question here. When I did that Jenkins could once again detect the container it's running inside.
An update was probably released for Ubuntu 21.10 switching to cgroup v2, just as I posted the question, since I could later reproduce the issue there as well.
Related Topics
Parsing Shell Script Arguments
Cuda Performance Penalty When Running in Windows
Find and Remove Files with Space Using Find Command on Linux
Using Output of Awk to Run Command
Who Can Access a File with Octal Permissions "000" on Linux/Unix
Bash: Head & Tail Behavior with Bash Script
Where Does Output of Print in Kernel Go
How to See Contents of Hive Orc Files in Linux
Hadoop: Require Root's Password After Enter "Start-All.Sh"
Postgresql on Elastic Beanstalk (Amazon Linux 2)
What Is Terminal Escape Sequence for Ctrl + Arrow (Left, Right,...) in Term=Linux
Ps: Clean Way to Only Get Parent Processes
How to Send Multicast Packets via a Specfic Interface in Linux
How to Pass a Value to a Builtin Linux Kernel Module at Boot Time
How to Create a JSON Web Token (Jwt) Using Openssl Shell Commands