In lxd, how to find containers by searching for process containers

In lxd, how to find containers by searching for process containers

Similar to docker commands such as: docker ps -q | xargs docker inspect --format ‘{{.State.Pid}}, {{.Name}}’ | grep “PID”

Assuming I understood your question properly, ps faux from the host should let you find which container has a given process in it:

$ ps faux | tail -n 23
root        3415  0.0  0.1 1125296 14436 ?       Ss   02:02   0:05 [lxc monitor] /var/snap/lxd/common/lxd/containers smb
1065536     3482  0.0  0.0  99620  7552 ?        Ss   02:02   0:03  \_ /sbin/init
1065536     3712  0.0  0.1  39476  9088 ?        Ss   02:02   0:00      \_ /lib/systemd/systemd-journald
1065636     3744  0.0  0.0  16068  5520 ?        Ss   02:02   0:00      \_ /lib/systemd/systemd-networkd
1065637     3889  0.0  0.1  25216  9756 ?        Ss   02:02   0:00      \_ /lib/systemd/systemd-resolved
1065536     3978  0.0  0.0   9464  1604 ?        Ss   02:02   0:00      \_ /usr/sbin/cron -f -P
1065640     3996  0.0  0.0 152768  3176 ?        Ssl  02:02   0:00      \_ /usr/sbin/rsyslogd -n -iNONE
1065536     4655  0.0  0.1  86700 15352 ?        Ss   02:03   0:00      \_ /usr/sbin/smbd --foreground --no-process-group
1065536     4666  0.0  0.1  84576  8372 ?        S    02:03   0:00          \_ /usr/sbin/smbd --foreground --no-process-group
1065536     4667  0.0  0.0  84568  5724 ?        S    02:03   0:00          \_ /usr/sbin/smbd --foreground --no-process-group
root        3802  0.0  0.1 1125296 14580 ?       Ss   02:02   0:04 [lxc monitor] /var/snap/lxd/common/lxd/containers squid
1589824     4018  0.0  0.0  99580  7504 ?        Ss   02:02   0:02  \_ /sbin/init
1589824     4197  0.0  0.1  31284  8912 ?        Ss   02:02   0:00      \_ /lib/systemd/systemd-journald
1589924     4245  0.0  0.0  16068  5488 ?        Ss   02:02   0:00      \_ /lib/systemd/systemd-networkd
1589925     4278  0.0  0.1  25480  9964 ?        Ss   02:02   0:00      \_ /lib/systemd/systemd-resolved
1589824     4281  0.0  0.0   9464  1628 ?        Ss   02:02   0:00      \_ /usr/sbin/cron -f -P
1589928     4285  0.0  0.0 152768  3108 ?        Ssl  02:02   0:00      \_ /usr/sbin/rsyslogd -n -iNONE
1589824     4347  0.0  0.1  63968 11996 ?        Ss   02:02   0:00      \_ /usr/sbin/squid --foreground -sYC
1589837     4353  0.0  0.6 110640 55732 ?        Sl   02:02   0:07          \_ (squid-1) --kid squid-1 --foreground -sYC
1589837     4365  0.0  0.0   6060  1576 ?        S    02:02   0:00              \_ (logfile-daemon) /var/log/squid/access.log
nut       154588  0.0  0.0   5464  2312 ?        Ss   05:35   0:00 /lib/nut/upsd
sdeziel   684990  1.5  0.1  18624  9968 ?        Ss   18:48   0:00 /lib/systemd/systemd --user
sdeziel   684991  0.0  0.0 103180  3468 ?        S    18:48   0:00  \_ (sd-pam)

In the above, we see the smbd daemon executes inside the container named smb. This is identifiable by the parent process [lxc monitor] /var/snap/lxd/common/lxd/containers smb.

HTH,
Simon

Yes your method is right, thanks. But I have more than 500 containers, I quickly find that container by this command。

grep -rn PID /sys/fs/cgroup/pids/*

That’s right, TIL :slight_smile: Thanks!

$ grep -lwm1 4655 /sys/fs/cgroup/pids/lxc.payload.*/*/*/tasks | sed 's/.*lxc\.payload\.//; s/\/.*//'
smb