Docker 101 : Accessing the container's namespace using Docker commands and Linux command - ip netns exe, docker exec



As we can see in the below diagram, the container's network namespace and the host's network namespace are connected via a Linux virtual bridge "docker0":


Usually Linux and its running programs put information about network namespace in the "/proc/pid/net" directory and also in the "/var/run/netnsdirectory.

Docker does not automatically write that information there, we would need to do it manually to be able to use the command "ip netns exec" in the docker's container namespace.

We start by retrieving the container ID using the below command:


Now that we have the container ID, we can use it to get the container's process ID as follows:


We then create a symbolic link to the container’s network namespace as follows:


And then we check the IP address of the container using the "ip netns exec" command:


We try now to see if the "docker exec" command is going to return the same information:


We see that both commands return the same IP address.

Comments

Leave as a comment:

Archive