Skip to content

Instantly share code, notes, and snippets.

@slp
Created August 24, 2017 09:25
Show Gist options
  • Save slp/24fee8457b765109a967e93012a4f0a6 to your computer and use it in GitHub Desktop.
Save slp/24fee8457b765109a967e93012a4f0a6 to your computer and use it in GitHub Desktop.
Peeking into a container's namespace using system utils
[root@rhel74-docker ~]# docker run -ti --rm alpine
Unable to find image 'alpine:latest' locally
Trying to pull repository registry.access.redhat.com/alpine ... 
Trying to pull repository docker.io/library/alpine ... 
latest: Pulling from docker.io/library/alpine
88286f41530e: Pull complete 
Digest: sha256:1072e499f3f655a032e88542330cf75b02e7bdf673278f701d7ba61629ee3ebe
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link 
       valid_lft forever preferred_lft forever
/ # ps -ef
PID   USER     TIME   COMMAND
    1 root       0:00 /bin/sh
    6 root       0:00 ps -ef
/ # 

(...on a different terminal...)

[root@rhel74-docker ~]# lsns
        NS TYPE  NPROCS   PID USER COMMAND
4026531836 pid      121     1 root /usr/lib/systemd/systemd --switched-root --system --deserialize 21
4026531837 user     122     1 root /usr/lib/systemd/systemd --switched-root --system --deserialize 21
4026531838 uts      121     1 root /usr/lib/systemd/systemd --switched-root --system --deserialize 21
4026531839 ipc      121     1 root /usr/lib/systemd/systemd --switched-root --system --deserialize 21
4026531840 mnt      114     1 root /usr/lib/systemd/systemd --switched-root --system --deserialize 21
4026531856 mnt        1    17 root kdevtmpfs
4026531956 net      120     1 root /usr/lib/systemd/systemd --switched-root --system --deserialize 21
4026532154 mnt        1   496 root /usr/lib/systemd/systemd-udevd
4026532209 mnt        1   671 root /usr/sbin/NetworkManager --no-daemon
4026532213 mnt        3  1396 root /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/
4026532217 mnt        1  1614 root /bin/sh
4026532218 uts        1  1614 root /bin/sh
4026532219 ipc        1  1614 root /bin/sh
4026532220 pid        1  1614 root /bin/sh
4026532222 net        1  1614 root /bin/sh
4026532285 net        1  1630 root /usr/lib/systemd/systemd-machined
4026532344 mnt        1  1630 root /usr/lib/systemd/systemd-machined
[root@rhel74-docker ~]# nsenter -t 1614 -m -p -n /bin/sh
/ # export PATH=$PATH:/usr/bin:/usr/sbin:/bin:/sbin
/ # ps -ef
PID   USER     TIME   COMMAND
    1 root       0:00 /bin/sh
    8 root       0:00 /bin/sh
    9 root       0:00 ps -ef
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link 
       valid_lft forever preferred_lft forever
/ # 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment