When I create a new docker container like with
docker run -it -m 560m --cpuset-cpus=1,2 ubuntu sleep 120
and check its namespaces, I can see that new namespaces have been created (example for pid 7047).
root@dude2:~# ls /proc/7047/ns -la
total 0
dr-x--x--x 2 root root 0 Jul 7 12:17 .
dr-xr-xr-x 9 root root 0 Jul 7 12:16 ..
lrwxrwxrwx 1 root root 0 Jul 7 12:17 ipc -> ipc:[4026532465]
lrwxrwxrwx 1 root root 0 Jul 7 12:17 mnt -> mnt:[4026532463]
lrwxrwxrwx 1 root root 0 Jul 7 12:17 net -> net:[4026532299]
lrwxrwxrwx 1 root root 0 Jul 7 12:17 pid -> pid:[4026532466]
lrwxrwxrwx 1 root root 0 Jul 7 12:17 user -> user:[4026531837]
lrwxrwxrwx 1 root root 0 Jul 7 12:17 uts -> uts:[4026532464]
root@dude2:~# ls /proc/self/ns -la
When I check with ip netns list
I cannot see the new net namespace.
dude@dude2:~/docker/testroot$ ip netns list
dude@dude2:~/docker/testroot$
Any idea why?
As @jary indicates, the
ip netns
command only works with namespace symlinks in/var/run/netns
. However, if you you have thensenter
command available (part of theutil-linux
package), you can accomplish the same thing using the PID of your docker container.To get the PID of a docker container, you can run:
To get a command inside the network namespace of a container:
E.g:
The above was equivalent to running
ip netns exec <some_namespace> ip addr show
.As you can see here, you will need to run
nsenter
with root privileges.Similar but different with @jary’s answer.
There is no need to introduce
/proc/<pid>/
ornetster
. Only one move below to achieve what you want. Thus, you could operate containers’ network namespace just like they are created manually on host machine.One Move:
Result:
Start a container:
List container:
List network namespace of this container:
Delete container:
List network namespace again:
That's because docker is not creating the reqired symlink:
Then, the container's netns namespace can be examined with
ip netns ${container_id}
, e.g.: