Creating Docker Networks for Container Communication
When containers run on the same Docker host, they need a way to find and talk to each other. Docker networks provide exactly that — an isolated communication layer where containers can reach each other by name instead of by IP address. Without a custom network, containers on the default bridge can only communicate by IP, which breaks when containers restart and get new IPs.
Why Use Custom Docker Networks?
- DNS resolution: Containers on a custom network can ping each other by container name. The default bridge network does not provide this.
- Isolation: Only containers explicitly connected to a network can communicate through it. This keeps unrelated services separated.
- Portability: Compose files and scripts can reference container names instead of hardcoding IPs.
Step 1: Create a Docker Network
docker network create app-network
This creates a bridge network (the default driver). You can verify it exists:
docker network ls
You will see your new network listed alongside the default networks (bridge, host, none).
Step 2: Run Containers on the Network
Start two containers on the same network. Here is a practical example with a Node.js app and a PostgreSQL database:
# Start a PostgreSQL database on the app-network
docker run -d --name app-db --network app-network \
-e POSTGRES_PASSWORD=secret \
postgres:16
# Start a Node.js app on the same network
docker run -d --name app-server --network app-network \
-p 3000:3000 \
my-node-app:latest
Both containers are now on app-network. The Node.js app can connect to the database using app-db as the hostname — no IP address needed.
Step 3: Test Connectivity
Exec into one container and ping the other by name:
docker exec -it app-server /bin/bash
ping app-db
If the ping succeeds, DNS resolution is working. Your application can now use app-db as the database host in its connection string.
Step 4: Inspect a Network
To see which containers are connected to a network and their assigned IPs:
docker network inspect app-network
This outputs the network configuration including the subnet, gateway, and all connected containers with their IP addresses.
Step 5: Connect and Disconnect Running Containers
You can add a running container to a network without restarting it:
# Connect a running container to a new network
docker network connect app-network some-existing-container
# Disconnect a container from a network
docker network disconnect app-network some-existing-container
This is useful when you need to temporarily give a container access to another network for debugging.
Network Drivers
| Driver | Use Case |
|---|---|
bridge | Default. Containers on the same host communicate over an internal network. |
host | Container shares the host's network stack. No isolation, but no port mapping overhead. |
overlay | Multi-host networking for Docker Swarm or Kubernetes. Containers on different hosts communicate. |
none | No networking. The container is completely isolated. |
Common Mistakes
- Using the default bridge: New users often skip creating a custom network. The default bridge does not support DNS resolution between containers — always create a custom network for multi-container setups.
- Hardcoding IPs: Do not hardcode container IPs in your application config. Use container names and let Docker's DNS handle resolution.
- Forgetting the network in docker run: If you create a network but forget
--networkwhen running containers, they will land on the default bridge and cannot resolve each other by name.
Practical Example: App + Database + Cache
# Create the network
docker network create backend
# Start PostgreSQL
docker run -d --name db --network backend \
-e POSTGRES_PASSWORD=secret postgres:16
# Start Redis
docker run -d --name cache --network backend redis:7
# Start the app — connects to both db and cache by name
docker run -d --name app --network backend \
-p 3000:3000 my-app:latest
Your application code can now connect to the database at db:5432 and the cache at cache:6379.