Learn how Docker networks work, how containers communicate with each other by name, and how port mapping exposes services to the host.
Docker Networking
Every container gets its own network namespace - its own virtual network interface, IP address, and routing table. Docker connects containers together (and to the outside world) through networks. Understanding how Docker networking works is essential for building multi-container applications where your web server needs to talk to your database, or your API needs to reach a message queue.
Network Drivers
Docker ships with several built-in network drivers. The driver determines how containers are connected at the OS level.
The vast majority of development and single-host production work uses bridge networks.
The Default Bridge Network
When you start a container without specifying a network, it is attached to the default bridge network (named bridge, backed by the docker0 Linux bridge interface). This is the network Docker creates automatically. The docker0 interface lives on the machine actually running the Docker Engine - on Linux that is your host; on macOS and Windows, Docker Desktop runs the Engine inside a Linux VM, so docker0 is inside that VM, not on the host itself.
Containers on the default bridge can communicate by IP address but not by name. This is the main limitation of the default bridge network and the reason you should always create custom networks.
Custom Bridge Networks
A user-defined bridge is a named network you create yourself. It has one critical advantage over the default bridge: automatic DNS resolution by container name.
Connecting containers to a custom network
Containers on the same user-defined bridge can resolve each other by container name or by the network alias.
Inspecting a network
Port Mapping
Containers on a bridge network are isolated from the host. To reach a service from your browser or from external clients, you must publish a port using -p.
After this, requests to http://localhost:8080 on the host are forwarded to port 80 in the container.
Port mapping variants
Binding to 127.0.0.1 is important for databases and admin panels - it prevents external machines on your network from connecting to a service that should only be accessed locally.
The host Network Driver
With --network host, the container's processes bind directly to the host's network interfaces. There is no NAT, no port mapping - if the container listens on port 8080, it is immediately reachable on the host's port 8080.
Pros:
- Lowest possible network latency
- No need to manage port mappings
Cons:
- No network isolation - container can see all host network traffic
- Port conflicts between containers
- Not available on Docker Desktop (macOS/Windows) - only works on Linux
The none Network Driver
A container with --network none has no network interfaces except loopback. It cannot initiate or receive network connections.
Use none for batch processing jobs that handle sensitive data and should never phone home, or for containers that communicate exclusively via shared volumes.
Container-to-Container Communication by Name
The key DNS rule for user-defined networks:
- Containers resolve each other by container name (
--name) - Containers can also be given network aliases - alternative DNS names on that network
Aliases are particularly useful when you want to swap out the underlying container (e.g., switch from Postgres to a Postgres replica) without changing application configuration.
Connecting a Container to Multiple Networks
A container can be attached to more than one network simultaneously, allowing it to communicate with different groups of containers while keeping those groups isolated from each other.
Cleaning Up Networks
Practical Example: Web App + Database + Redis
Let us wire up a realistic three-service setup entirely with network commands (before we get to Docker Compose):
Inside nginx.conf, you would reference the upstream as http://api:3000 - Docker's DNS resolves api to the API container's IP within the app-stack network.
DNS Resolution Summary
Troubleshooting Network Issues
Custom bridge networks with DNS resolution are one of Docker's most useful features. In the next chapter we will see how Docker Compose manages networks automatically, removing the need to run all these commands by hand.