Docker Networking Modes
When you run a container, Docker provides several networking modes to tailor connectivity to your application’s needs:None Network
With the “none” network mode, the container is not attached to any network. As a result, it cannot communicate with external systems, nor can external systems reach the container. For example:Multiple containers started in the none network mode remain completely isolated from each other and the external network.
Host Network
In the host network mode, the container shares its host’s network stack. This means there is no network isolation between the host and the container. For instance, if a web application inside the container listens on port 80, that application immediately becomes accessible on port 80 of the host. However, running a second container that also attempts to bind to the same port will result in a failure because two processes cannot share the same port on the host. For example:Bridge Network
The default Docker networking mode is the bridge network. When Docker is installed, it automatically creates an internal private network called “bridge” (visible as “docker0” on the host) with a default subnet (usually 172.17.0.0/16). Each container connected to this network receives a unique IP address from this subnet. For example, running two containers:b3165.... To view the network namespace details associated with a container, inspect the container details with:
Container Attachment to the Bridge Network
Docker attaches each container to the bridge network by creating a pair of virtual interfaces—essentially a virtual cable with an interface at each end. One interface is connected to the host’s “docker0” bridge, and the other interface is placed inside the container’s network namespace. Inspect the interfaces on the Docker host using:b3165c10a92b), run:
- Creates a new network namespace.
- Establishes a pair of virtual interfaces.
- Attaches one end to the container’s namespace and the other to the “docker0” bridge.
- Assigns an IP address to the container’s interface.
Port Mapping
Consider a scenario where a container runs an nginx web application that listens on port 80. By default, since the container runs in a private network segment, only other containers on the same network or the host can access application endpoints. Port mapping (port publishing) in Docker enables external access by mapping a port on the host to a port on the container. For example, to map port 8080 on the host to port 80 within the container:-
Accessing the container directly via its IP on port 80 might result in a failure:
-
However, accessing the application using the host’s IP and port 8080 should succeed:
How Port Forwarding Works
Docker employs IP tables to implement port forwarding. This mechanism adds a Network Address Translation (NAT) rule to translate traffic arriving on a specific host port to the corresponding container port. For example, Docker might add a rule similar to the following:Conclusion
In summary, Docker networking offers multiple modes—none, host, and bridge—to manage connectivity for containers. The default bridge network uses a virtual switch (docker0) to connect containers via dedicated network namespaces, while port mapping enables external access by forwarding traffic from a designated host port to the container’s port.For a deeper dive into container networking, consider exploring the Kubernetes Networking Basics and other resources in the Docker Documentation.