Bài gốc: Revisit Docker and review what we learned — tác giả BaconYao, lấy qua Notion public API.

Key Takeaway

In this chapter, we will use Docker to review the techniques we previously learned, including Network Namespace, Virtual Ethernet, Linux Bridge, and more.

Quick Review

Experiment

Set Up the Experiment Environment

This environment uses the Ubuntu 24.04 server provided by iximiuz Labs, which offers a fast and convenient online practice environment.Reference: https://labs.iximiuz.com/

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> nginx to 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 docker0 bridge.
  • Step 3: Use the $ ip route command to check the host’s routing table. You can observe that the docker0 rule, like eth0, 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 3152 and 3281.
  • Step 2: Use the command sudo nsenter -n -t 3152 to enter the network namespace of the ng1 container. The method for entering ng2 is similar.
  • Step 1: After entering the network namespaces of the two containers, we ping the ng2 container directly from the ng1 container. The experiment shows that the two containers can communicate with each other.
  • Step 1: This time, we observe the packets passing through the docker0 bridge from the Host.
  • Step 2: Once again, we ping the ng2 container directly from the ng1 container.
We can observe that both ARP and ICMP packets are being transmitted and received normally.
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 ng1 container’s network namespace, ping 8.8.8.8.
  • Step 2: Use the command sudo iptables-save -t filter -c to observe the packet and byte counters for Docker’s custom chains.
You can see that the packet and byte counts for the Docker channels increase with each ping.
In this experiment, we attempt to delete one of Docker’s iptables rules.
  • Step 1: Delete the Docker iptables rule.
You will notice that the previously working 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 ng1 container can successfully communicate with 8.8.8.8.

Conclusion

In this chapter, we conducted simple experiments using Docker to verify and review the concepts we previously learned. This also lays a solid foundation for learning Docker and related networking technologies in the future.