Bài gốc: Revisit Docker and review what we learned — tác giả BaconYao, lấy qua Notion public API.
Key Takeaway
Quick Review
- If you want to understand the changes in iptables after installing Docker, please refer to this article: 06. Understanding the Relationship Between iptables, Docker, and Linux Bridge
- If you want to understand the relationship between Docker and Network Namespace, please refer to this article: 01. Network Namespace and Virtual Ethernet (VETH)
Experiment
Set Up the Experiment Environment
Install Docker
Install following the official guide: Docker DocumentationUbuntu After installation, the Docker service is not enabled by default, so we need to enable it using the following command:Let’s begin the experiment!

-
Step 1: Use the command
sudo docker run -d --name <ng1/ng2> nginxto create two nginx Docker containers. -
Step 2: After creating the two nginx containers, you will notice that two additional virtual ethernet interfaces have been added, both connected to the
docker0bridge. -
Step 3: Use the
$ ip routecommand to check the host’s routing table. You can observe that thedocker0rule, likeeth0, is created automatically by the kernel.

Next, let’s enter the network namespaces of the two containers to check the IP addresses assigned to them.Here, we use the
nsenter command to enter the network namespace. If you prefer to use the ip netns approach, you can refer to this article: Docker and Network Namespace-
Step 1: Retrieve the process IDs of the two containers, which are
3152and3281. -
Step 2: Use the command
sudo nsenter -n -t 3152to enter the network namespace of theng1container. The method for enteringng2is similar.

- Step 1: After entering the network namespaces of the two containers, we ping the
ng2container directly from theng1container. The experiment shows that the two containers can communicate with each other.

-
Step 1: This time, we observe the packets passing through the
docker0bridge from the Host. -
Step 2: Once again, we ping the
ng2container directly from theng1container.

As a side note, how can we tell which virtual ethernet interface on the Host corresponds to the container’s network namespace?From the diagram on the left, we can see that the interface number
12 on the Host (Bottom) corresponds to the eth0 interface inside the container (Up), which also ends with 12.
Now, let’s take a look at the iptables filter table.
-
Step 1: From the
ng1container’s network namespace, ping8.8.8.8. -
Step 2: Use the command
sudo iptables-save -t filter -cto observe the packet and byte counters for Docker’s custom chains.

In this experiment, we attempt to delete one of Docker’s iptables rules.
- Step 1: Delete the Docker iptables rule.
ping 8.8.8.8 command stops. This indicates that packets can no longer be forwarded through the docker0 bridge, and therefore cannot reach eth0 or communicate with the outside network.
- Step 1: After adding the rule back, packets can be sent again, and the
ng1container can successfully communicate with8.8.8.8.