Docker Container Networking: Modes, Configuration, and Practical Commands
This article explains Docker's built‑in container networking, describing the three default networks, the four network modes (bridge, container, host, none), how to inspect and configure them with commands, use veth pairs and network namespaces, and perform common container operations such as port mapping and custom bridge creation.
Docker Container Network Overview
Docker creates three default networks (bridge, host, none) that can be listed with docker network ls . Each container receives a Container‑IP from the docker0 bridge and uses it as the default gateway.
# docker network ls
NETWORK ID NAME DRIVER SCOPE
cd97bb997b84 bridge bridge local
0a04824fc9b6 host host local
4dcb8fbdb599 none null localDocker's Four Network Modes
Mode
Configuration
Description
host
--network host
Container shares the host's network namespace.
container
--network container:NAME_OR_ID
Container shares another container's network namespace.
none
--network none
Container has its own namespace but no network devices are configured.
bridge
--network bridge (default)
Container connects to the docker0 bridge.
Bridge Mode Details
Docker creates a virtual bridge docker0 on the host. Containers attach to this bridge, receive IPs from its subnet, and can communicate directly via their Container‑IP.
Port mapping is performed with -p which adds DNAT rules in iptables.
# docker run -it --name myapp -p 8080:80 nginxContainer Mode Details
Two containers share the same network namespace, thus the same IP and ports, while file systems remain isolated.
# docker run -dit --name b3 busybox
# docker run -it --name b2 --network container:b3 busyboxHost Mode Details
The container uses the host's network stack directly, gaining the host's IP address and full network performance.
# docker run -it --network host busyboxNone Mode Details
Container gets only a loopback interface; no external connectivity unless manually configured.
# docker run -it --network none busybox ifconfig -aNetwork Namespace Operations
Linux ip netns commands create isolated namespaces. Example creates namespaces ns0 and ns1 , a veth pair, assigns IPs, and verifies connectivity.
# ip netns add ns0
# ip netns add ns1
# ip link add type veth
# ip link set veth0 netns ns0
# ip link set veth1 netns ns1
# ip netns exec ns0 ip addr add 192.0.0.1/24 dev veth0
# ip netns exec ns1 ip addr add 192.0.0.2/24 dev veth1
# ip netns exec ns0 ping -c 2 192.0.0.2Common Container Operations
View hostname: hostname
Set custom hostname: --hostname myhost
Specify DNS: --dns 114.114.114.114
Add host entry: --add-host example.com:1.1.1.1
Expose ports with -p (dynamic or fixed).
Custom Bridge Creation
Create a user‑defined bridge with a specific subnet and attach containers to it.
# docker network create -d bridge --subnet 192.168.2.0/24 --gateway 192.168.2.1 mybridge
# docker run -it --network mybridge busybox ifconfigTop Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.