Docker Networking
Docker networking allows containers to communicate with each other, the host system, and external networks. Understanding Docker networking is essential for deploying real-world applications.
1. Types of Docker Networks
Docker provides five types of networks:
Network Type | Default? | Description |
Bridge | ✅ Yes | Default network for standalone containers. Containers can communicate internally, but need port mapping to communicate externally. |
Host | ❌ No | Containers share the host’s networking stack (no isolation). |
Overlay | ❌ No | Used in Swarm mode to enable multi-host networking. |
Macvlan | ❌ No | Assigns a real MAC address to a container, making it look like a physical device. |
None | ❌ No | No network connectivity (fully isolated). |
2. Checking Existing Networks
To list all available Docker networks, run:
docker network ls
Example output:
NETWORK ID NAME DRIVER SCOPE
b7ef25d9a6d2 bridge bridge local
d1f49dc9f7cd host host local
a12d9c5d84a8 none null local
🛠️ 3. Bridge Network (Default)
By default, when you run a container, it connects to the bridge network.
docker network inspect bridge
This shows details like IP ranges and connected containers.
🔹 Running a container in bridge mode:
docker run -d --name webserver nginx
This container won’t be accessible externally unless you map ports:
docker run -d -p 8080:80 --name webserver nginx
Now you can access the Nginx server at http://localhost:8080
.
🛠️ 4. Creating a Custom Bridge Network
To create an isolated network for multiple containers:
docker network create mynetwork
To run a container in this network:
docker run -d --name app1 --network mynetwork nginx
Now, any container in mynetwork
can communicate with app1 by using its name:
ping app1
🛠️ 5. Host Network
The host network mode removes network isolation, meaning:
The container shares the host’s IP address.
There is no need for port mapping.
🔹 Running a container in host mode:
docker run --network host -d nginx
Now, Nginx runs directly on the host’s network and can be accessed at http://localhost
.
🔴 Security Risk: Containers can access all host services, making it less secure.
6. Connecting and Disconnecting Containers
Connect a running container to a network:
docker network connect mynetwork container1
Disconnect a container from a network:
docker network disconnect mynetwork container1
🛠️ 7. Inspecting a Network
To see detailed information about a network:
docker network inspect mynetwork
This will show:
Connected containers
Subnet & Gateway
IP Address assignment