Skip to content

Instantly share code, notes, and snippets.

@berndbausch
Last active February 28, 2018 08:07
Show Gist options
  • Save berndbausch/475c03c344b8c37b689afd5e560cc233 to your computer and use it in GitHub Desktop.
Save berndbausch/475c03c344b8c37b689afd5e560cc233 to your computer and use it in GitHub Desktop.
Find the container of a process, and the processes of a container

How to find containers of processes and vice versa

This is about Linux Containers, managed with LXC or LXD.

What are the processes of a container?

# lxc-info -p -n CONTAINERNAME                  # -p or --pid displays init's PID
PID:            27149
# pstree -p 27149
systemd(27149)─┬─agetty(27502)
               ├─agetty(27503)
               ├─agetty(27504)
               ├─agetty(27505)
               ├─agetty(27507)
               ├─cron(27339)
               ├─dhclient(27472)
               ├─rsyslogd(27334)─┬─{in:imklog}(27359)
               │                 ├─{in:imuxsock}(27358)
               │                 └─{rs:main Q:Reg}(27360)
               ├─sshd(27495)
               └─systemd-journal(27263)

A process added for example with lxc-console or lxc-execute also appears in the list.

What are the namespaces of a container?

# ls -l /proc/PID_OF_INIT/ns
total 0
lrwxrwxrwx 1 root root 0 Feb 28 14:11 cgroup -> cgroup:[4026532507]
lrwxrwxrwx 1 root root 0 Feb 28 14:11 ipc -> ipc:[4026532447]
lrwxrwxrwx 1 root root 0 Feb 28 14:11 mnt -> mnt:[4026532379]
lrwxrwxrwx 1 root root 0 Feb 28 14:11 net -> net:[4026532450]
lrwxrwxrwx 1 root root 0 Feb 28 14:11 pid -> pid:[4026532448]
lrwxrwxrwx 1 root root 0 Feb 28 14:13 user -> user:[4026531837]
lrwxrwxrwx 1 root root 0 Feb 28 14:11 uts -> uts:[4026532446]

ps -o mntns etc should also display namespaces, but it only shows a dash.

Which is the container of a given process?

List its namespaces and compare them with the namespaces of all containers.

To be continued

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