How to Configure Docker Networking with Bridge, Host, and Overlay Networks for Container Communication
Learning how to configure Docker networking with bridge, host, and overlay networks for container communication is one of the most important skills for any DevOps engineer or Linux administrator. Docker networking controls how containers talk to each other and to the outside world. Without a solid understanding of network modes, your containers may fail to communicate, expose ports incorrectly, or create security gaps. This tutorial walks you through each major Docker network type. You’ll create custom bridge networks, test host networking, and set up an overlay network for multi-host communication. By the end, you’ll know exactly which network type to use and when.
Prerequisites and Requirements for Docker Networking
Before you start configuring Docker networks, make sure you have the following in place.
Required software and access:
- A Linux server running Ubuntu 20.04 or later (two servers for overlay networking)
- Docker Engine 20.10 or newer installed on each server
- Docker Swarm initialized on at least one node (for overlay networks)
- Root or sudo access on all machines
- Basic familiarity with the Linux command line
Estimated time: 45–60 minutes
If you don’t have Docker installed yet, follow the official Docker installation guide for Ubuntu before continuing. Once Docker is running, confirm it’s working with docker --version. You should see a version number returned. You’re ready to move on once that command succeeds.
Step-by-Step Guide to Configure Docker Networking
For a related walkthrough, see: How to Tune WordPress Performance Using Advanced Wp-config.php Configuration Constants
This section covers all three network types in order. Work through each section carefully.
Step 1: Inspect the default Docker networks
Docker creates three networks automatically at installation. Run this command to list them:
docker network ls
You’ll see bridge, host, and none listed. The default bridge network connects containers on the same host. However, it doesn’t support automatic DNS resolution between containers by name. That’s why you’ll create a custom bridge next.
Step 2: Create a custom bridge network
Custom bridge networks are the recommended way to connect containers on a single host. Run:
docker network create --driver bridge my-bridge-network
Verify it was created:
docker network inspect my-bridge-network
This output shows the subnet, gateway, and connected containers. Docker assigns a default subnet automatically. You can also specify your own:
docker network create
--driver bridge
--subnet 192.168.10.0/24
--gateway 192.168.10.1
custom-bridge
Step 3: Connect containers using the custom bridge
Start two containers on your custom bridge network:
docker run -d --name web --network my-bridge-network nginx
docker run -d --name app --network my-bridge-network alpine sleep 3600
Now test communication between them. Enter the app container:
docker exec -it app sh
Inside the container, ping the web container by name:
ping web
You’ll see successful replies. This works because custom bridge networks include an embedded DNS server. Exit the container with exit.
Step 4: Test host networking mode
Host networking removes the network isolation between the container and the Docker host. The container shares the host’s network stack directly. This is useful for performance-sensitive applications.
Run a container with host networking:
docker run -d --network host --name host-nginx nginx
Nginx now listens on port 80 of your actual host , not a container port mapping. Verify with:
curl http://localhost
You should see the Nginx welcome page. Note that host networking only works on Linux. It’s not supported on Docker Desktop for Mac or Windows.
Step 5: Initialize Docker Swarm for overlay networking
Overlay networks connect containers across multiple Docker hosts. They require Docker Swarm mode. On your primary server, run:
docker swarm init --advertise-addr YOUR_SERVER_IP
Replace YOUR_SERVER_IP with your actual IP address. Docker returns a join token. Copy that token. On your second server, run the join command Docker provides. It looks like this:
docker swarm join --token SWMTKN-1-xxxxx YOUR_SERVER_IP:2377
Confirm both nodes are in the swarm on the primary server:
docker node ls
Both servers should appear with a Ready status.
Step 6: Create an overlay network
Now create an overlay network that spans both hosts:
docker network create
--driver overlay
--attachable
my-overlay-network
The --attachable flag lets standalone containers join the network, not just Swarm services. Inspect the network:
docker network inspect my-overlay-network
You’ll see the scope listed as swarm, confirming it spans the entire cluster.
Step 7: Deploy a service using the overlay network
Deploy a replicated Nginx service across your swarm:
docker service create
--name web-service
--network my-overlay-network
--replicas 2
-p 8080:80
nginx
Check that both replicas are running:
docker service ps web-service
You’ll see one replica on each node. Both containers can communicate over the overlay network regardless of which physical host they’re on. For deeper reading on overlay networking internals, see the Docker overlay network documentation.
Step 8: Connect a container to multiple networks
A single container can join more than one network. This is useful when a container needs to talk to both an internal app network and a public-facing network.
docker network connect my-bridge-network web-service
Disconnect it just as easily:
docker network disconnect my-bridge-network web-service
This gives you fine-grained control over which containers can talk to each other.
Troubleshooting Common Docker Networking Issues
Even experienced admins run into networking problems. Here are the most common ones.
Containers can’t ping each other by name
This happens when containers are on the default bridge network. Move them to a custom bridge network. The default bridge doesn’t support container DNS.
Port conflicts with host networking
If port 80 is already in use on the host, your container won’t start. Check what’s using the port:
sudo ss -tlnp | grep :80
Stop the conflicting service or use a different port.
Overlay network not visible on second node
The overlay network only propagates when a service or attachable container uses it. Make sure you used the --attachable flag when creating the network. Also confirm both nodes are in an active swarm state.
Swarm join token expired
Tokens don’t expire by default, but you can rotate them. Generate a new worker token with:
docker swarm join-token worker
Containers lose network access after host reboot
Docker restarts containers with the --restart always flag. Make sure your containers are started with that option if you need them to survive reboots.
Conclusion
You now know how to configure Docker networking with bridge, host, and overlay networks for container communication across single and multi-host environments. You created custom bridge networks for local container DNS, tested host networking for direct host access, and deployed overlay networks for swarm-based multi-host setups. These three network types cover the vast majority of real-world Docker use cases. From here, you can explore Docker Compose networking to manage multi-container apps more easily, or look into Kubernetes networking if you’re scaling beyond Swarm. Understanding Docker networking at this level gives you the foundation to build reliable, well-isolated container infrastructure on any Linux server.
