What I Wanted to Do
Understand how to configure networking between Docker containers. Network settings become critical when running multiple containers with docker-compose.
Network Types
bridge (Default)
docker run -d --network bridge nginx
- The default network mode
- Containers can communicate with each other via IP address
- Runs in a separate network space from the host
host
docker run -d --network host nginx
- Uses the host machine’s network directly
- No port mapping needed
- Linux only (behavior differs on Mac and Windows)
none
docker run -d --network none nginx
- No network at all
- Completely isolated from external communication
Creating a Custom Network
docker network create mynetwork
docker run -d --network mynetwork --name app1 nginx
docker run -d --network mynetwork --name app2 nginx
Containers on the same network can communicate using container names.
# Connect from app1 to app2
curl http://app2
Network Configuration in docker-compose
services:
web:
image: nginx
networks:
- frontend
db:
image: mysql:8
networks:
- backend
app:
image: myapp
networks:
- frontend
- backend
networks:
frontend:
backend:
Commonly Used Commands
docker network ls # List networks
docker network inspect mynetwork # Inspect network details
docker network create mynetwork # Create a network
docker network rm mynetwork # Remove a network
docker network connect mynetwork <container> # Add container to network
Key Points
- docker-compose automatically creates a custom network
- Containers must be on the same network to communicate by name
- nginx 502 errors are often caused by incorrect network configuration
If you encounter nginx 502 errors, check nginx 502 Bad Gateway: Causes and Fixes for hostname configuration issues.
Related Articles
- How to Use docker-compose
- Docker Basic Commands Cheatsheet
- nginx 502 Bad Gateway: Causes and Fixes
- How to Persist Data with Docker Volumes
Recommended Cloud Hosting
Looking for reliable cloud infrastructure? Check out these developer-friendly services.
- Cherry Servers - High-performance VPS and dedicated servers
- Cloudways - Managed cloud hosting for developers