Understanding Docker0 and Custom Docker Networks: Practical Examples
This article walks through Docker's default docker0 bridge, explains how containers obtain IP addresses via veth‑pair interfaces, demonstrates inspecting container IPs, and shows how to create and use custom bridge networks so containers can communicate by name or IP, including adding existing containers to the new network.
Understanding docker0
After installing Docker on CentOS 7, the author runs a Tomcat container to explore how Docker handles container networking. docker run -d --name tomcat01 tomcat From the host, pinging the container's IP succeeds, confirming connectivity.
To retrieve a container's IP address, the command used is: docker inspect 3aaf where 3aaf is the container ID or name. The host's network interfaces are listed with: ip addr Each newly started container receives an IP address on the docker0 bridge, which is implemented using a veth‑pair. Starting a second container shows an additional pair of virtual interfaces: docker run -d --name tomcat02 tomcat Both containers can ping each other, demonstrating that they share the same bridge.
If the ping command is missing inside a container, the author installs it with:
apt update
apt install iputils-pingKey conclusions:
All containers without an explicit network use the default docker0 bridge.
Docker automatically assigns a free IP address to each container.
Custom Docker Networks
In micro‑service scenarios, accessing containers by name or migrating IPs is often required, which the default bridge does not support.
Listing all Docker networks shows the available drivers:
docker network lsThe default bridge driver corresponds to docker0. It does not support name resolution, and using --link is discouraged.
To create a custom bridge network with a specific subnet and gateway:
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynetAfter creation, the network appears in the list and can be inspected:
docker network inspect mynetContainers launched with the custom network can communicate by both IP and container name:
docker run -d -P --name tomcat-mynet-01 --net mynet tomcat
docker run -d -P --name tomcat-mynet-02 --net mynet tomcatThese two containers are mutually reachable via name or IP.
To connect an already running container (e.g., tomcat01) to the custom network:
docker network connect mynet tomcat01After connecting, the container can communicate with the other containers on mynet, completing the custom network setup.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
