Find container for process ID?

Is there a way to find out in which container a process is running?

Lets say I have a bunch of webserver containers running. They shows up on the host as numerous apache2 processes. Suppose one PID is eating my CPU and so would like to know which container that process belongs to.

Here is one way:

bash-shell# cd /sys/fs/cgroup/memory/lxc && for i in $(echo */); do echo $i && cat $i/cgroup.procs; done

cgroup.procs is an empty file…doesn’t return anything when I cat it…
Running updated Ubuntu LTS 16.04 with LXD 2.0.10…hmmm on my LXD 2.16 host (another host) I do get PID’s…

Ok I’ll update LXD first then and try again. Thanks for the hint…

This is with LXD 4.0.0, my guess it is the same as 3.11
say 5467 is process id outside of container then do

grep -rn 5467 /sys/fs/cgroup/pids/*
3 Likes

I did not find my process like that, but with:

grep 4569 -rn /sys/fs/cgroup/*/cgroup.procs

(lxd version 5.10)

The best I came up with after all this time is:
grep 25707 -rni /sys/fs/cgroup 2> /dev/null