Tech
Understanding Docker Networking: Bridge, Host, and Port Mapping
A beginner-friendly guide to Docker networking drivers and port forwarding. Learn how containers communicate using bridge, host, and overlay networks, and how to resolve services via DNS.
June 2026 · 5 min read · 1 views · 0 hearts
Advertisement
Imagine your Docker container is a high-end apartment in a massive skyscraper. Inside, everything works perfectly, but if you want to receive a package from the outside world or talk to the neighbor in apartment 4B, you can't just shout through the walls. You need a lobby, a mailing address, and a secure intercom system.
That is exactly what Docker networking does. It manages how containers talk to each other, how they reach the internet, and how the outside world reaches them.
The Basics: How Containers Communicate
By default, containers are isolated. This is a security feature. To let them communicate, Docker uses Network Drivers. Think of a driver as a specific "communication rulebook" that determines who can talk to whom.
The most common drivers you'll encounter are:
1. The Bridge Network (The Default)
When you start a container without specifying a network, it lands on the bridge network. This is a private internal network created by Docker on your host machine.
- How it works: Containers on the same bridge network can talk to each other using IP addresses.
- The Catch: In the default bridge, you have to use IPs (which change every time a container restarts). To use human-readable names (like
db-container), you must create a User-Defined Bridge Network.
2. The Host Network
The host network removes the isolation between the container and the Docker host.
- How it works: The container shares the host’s IP and port space directly. If the container runs a web server on port 80, it is immediately available on port 80 of your actual computer.
- Use Case: This is great for maximum performance (no network overhead) but risky for security since the container has full access to the host's network stack.
3. The None Network
This is the "dark room" of networking. The container has its own loopback interface but no external network access. It’s used for highly secure workloads that process data locally and don't need to touch the internet.
4. The Overlay Network
This is for the "big leagues." Overlay networks allow containers running on different physical hosts (like in a Docker Swarm or Kubernetes cluster) to communicate as if they were on the same local network.
Demystifying Ports: Mapping and Forwarding
One of the biggest hurdles for beginners is understanding why a container is "running" but the website isn't loading in the browser. This is usually a port mapping issue.
Containers have their own internal ports. For example, an Nginx container listens on port 80 inside the container. However, your laptop (the host) doesn't know about that internal port.
The Port Mapping Syntax
When you run a container, you use the -p flag to create a bridge between the host and the container:
docker run -p 8080:80 nginx
Breaking it down: Host Port : Container Port
* 8080 (Host): This is the port you type into your browser (localhost:8080).
* 80 (Container): This is the port the application is actually listening on inside the Docker environment.
Docker acts as a proxy, taking traffic from your laptop's port 8080 and forwarding it instantly to the container's port 80.
DNS and Service Discovery
If you have a Python app in one container and a PostgreSQL database in another, you don't want to hardcode an IP address like 172.17.0.2 because that IP will change the moment you restart the database.
This is where User-Defined Bridges shine. When you create your own network:
docker network create my-app-net
And attach your containers to it:
docker run --network my-app-net --name db postgres
docker run --network my-app-net --name web my-python-app
Docker provides a built-in DNS server. Your Python app can now simply connect to the host db instead of an IP address. Docker resolves db to the correct internal IP automatically.
Summary Checklist for Networking
| Scenario | Recommended Network | Key Detail |
|---|---|---|
| Simple local app | Default Bridge | Use -p to expose ports to your browser. |
| App + Database | User-Defined Bridge | Use container names for communication. |
| High Performance | Host Network | Direct access; no port mapping needed. |
| Multi-server cluster | Overlay Network | Bridges containers across different physical machines. |
| Maximum Security | None | Total isolation. |
Advertisement
Comments
Questions, corrections, and tips stay visible for everyone reading this page.
Join the discussion
No comments yet
Be the first to leave a note — it helps the next reader.