Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support container ID extraction for cgroup v2 #2121

Closed
sluebbert opened this issue Jun 26, 2023 · 1 comment
Closed

Support container ID extraction for cgroup v2 #2121

sluebbert opened this issue Jun 26, 2023 · 1 comment

Comments

@sluebbert
Copy link

Is your feature request related to a problem? Please describe.
When running a container through HashiCorp Nomad, or more broadly when running a container with cgroup v2 or --cgroupns private, the apm agent no longer detects that it is running in a container and does not transmit the container ID to the apm server.

Describe the solution you'd like
The apm agent should detect that it is running within a container and use alternative ways to detect the container ID when running with cgroup v2.

Describe alternatives you've considered
I've referenced this stack overflow post:
https://stackoverflow.com/questions/68816329/how-to-get-docker-container-id-from-within-the-container-with-cgroup-v2

I don't have full control over container creation. I am also not interested in exposing the Docker socket to the container. The responses on extracting from /proc/self/mountinfo seems to work in my case. See below for demo.

Additional context
Some general troubleshooting from a running container in Nomad:

root@555cdb12fe82:/app# cat /proc/self/mountinfo 
5115 4797 0:532 / / rw,relatime master:1802 - overlay overlay rw,lowerdir=/var/lib/docker/overlay2/l/7OXCTEI6H7K5SA3CEC7UG22KWG:/var/lib/docker/overlay2/l/63TR7IJFRJXOF5CCBEPQQZE5LY:/var/lib/docker/overlay2/l/VUVOJ4FDBVLVCN6HJQWNYPWZP4:/var/lib/docker/overlay2/l/E2IKF637QMZ27V3CFU2LNDYVM3:/var/lib/docker/overlay2/l/NLMOKICUFJEEIAEDULQQVVR5HD:/var/lib/docker/overlay2/l/GNMBMIPEBTUPW7CFBUOYPU22EV:/var/lib/docker/overlay2/l/SUG734QSUOTDKVRXFXMREG5Y34:/var/lib/docker/overlay2/l/WKBGGDG6XLZ3WEQMTLQR24JYJ2:/var/lib/docker/overlay2/l/OUBM7LFWRKR4EQ4RQR3IOI5WG4,upperdir=/var/lib/docker/overlay2/cfdfc4a13821992382526539607c3e4b0e237463d05f72c640ffe39f3cec34dc/diff,workdir=/var/lib/docker/overlay2/cfdfc4a13821992382526539607c3e4b0e237463d05f72c640ffe39f3cec34dc/work
5116 5115 0:535 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw
5117 5115 0:536 / /dev rw,nosuid - tmpfs tmpfs rw,size=65536k,mode=755,inode64
5118 5117 0:537 / /dev/pts rw,nosuid,noexec,relatime - devpts devpts rw,gid=5,mode=620,ptmxmode=666
5119 5115 0:425 / /sys ro,nosuid,nodev,noexec,relatime - sysfs sysfs ro
5120 5119 0:29 / /sys/fs/cgroup ro,nosuid,nodev,noexec,relatime - cgroup2 cgroup rw,nsdelegate,memory_recursiveprot
5121 5117 0:534 / /dev/mqueue rw,nosuid,nodev,noexec,relatime - mqueue mqueue rw
5122 5117 0:538 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,size=65536k,inode64
5123 5115 8:3 /opt/nomad/data/alloc/8d30a9e3-b039-55c2-bbc0-d0e42802edde/alloc /alloc rw,relatime - ext4 /dev/sda3 rw
5124 5115 8:3 /opt/nomad/data/alloc/8d30a9e3-b039-55c2-bbc0-d0e42802edde/service/local /local rw,relatime - ext4 /dev/sda3 rw
5125 5115 0:531 / /secrets rw,noexec,relatime - tmpfs tmpfs rw,size=1024k,inode64
5126 5115 8:3 /opt/nomad/data/alloc/8d30a9e3-b039-55c2-bbc0-d0e42802edde/service/local/settings0.json /app/appsettings.json rw,relatime - ext4 /dev/sda3 rw
5127 5115 8:17 /lib/docker/containers/555cdb12fe82402974029e20a4d3682e3598d784ba0ea3e4ace0bf72d16d05c8/resolv.conf /etc/resolv.conf rw,relatime - ext4 /dev/sdb1 rw
5128 5115 8:17 /lib/docker/containers/555cdb12fe82402974029e20a4d3682e3598d784ba0ea3e4ace0bf72d16d05c8/hostname /etc/hostname rw,relatime - ext4 /dev/sdb1 rw
5129 5115 8:3 /opt/nomad/data/alloc/8d30a9e3-b039-55c2-bbc0-d0e42802edde/hosts /etc/hosts rw,relatime - ext4 /dev/sda3 rw
4798 5116 0:535 /bus /proc/bus ro,nosuid,nodev,noexec,relatime - proc proc rw
4799 5116 0:535 /fs /proc/fs ro,nosuid,nodev,noexec,relatime - proc proc rw
4800 5116 0:535 /irq /proc/irq ro,nosuid,nodev,noexec,relatime - proc proc rw
4802 5116 0:535 /sys /proc/sys ro,nosuid,nodev,noexec,relatime - proc proc rw
4803 5116 0:535 /sysrq-trigger /proc/sysrq-trigger ro,nosuid,nodev,noexec,relatime - proc proc rw
4804 5116 0:539 / /proc/acpi ro,relatime - tmpfs tmpfs ro,inode64
4820 5116 0:536 /null /proc/kcore rw,nosuid - tmpfs tmpfs rw,size=65536k,mode=755,inode64
4821 5116 0:536 /null /proc/keys rw,nosuid - tmpfs tmpfs rw,size=65536k,mode=755,inode64
4822 5116 0:536 /null /proc/timer_list rw,nosuid - tmpfs tmpfs rw,size=65536k,mode=755,inode64
4839 5116 0:540 / /proc/scsi ro,relatime - tmpfs tmpfs ro,inode64
4840 5119 0:541 / /sys/firmware ro,relatime - tmpfs tmpfs ro,inode64
root@555cdb12fe82:/app# cat /proc/self/mountinfo | grep "/docker/containers/" | head -1 | awk '{print $4}' | sed 's/\/var\/lib\/docker\/containers\///g' | sed 's/\/resolv.conf//g'
/lib/docker/containers/555cdb12fe82402974029e20a4d3682e3598d784ba0ea3e4ace0bf72d16d05c8
root@555cdb12fe82:/app# cat /proc/self/cgroup 
0::/
@sluebbert
Copy link
Author

It looks like #2156 addressed this request.

Unfortunately, it also looks like I missed the fact that this container ID in the mountinfo belongs to the pause container in the Nomad allocation, not the container that is logging APM traffic.

In the end we are at least getting Elastic to recognize that the agent is running in a Docker container. It just doesn't help much in linking other sources of data that are using the correct service's container ID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant