Networking in Docker Compose
What you'll learn: How Docker Compose automatically creates networks and enables service-to-service communication by name.
Automatic Network Creation
When you run docker-compose up, Compose automatically creates a default bridge network for your project. All services defined in your docker-compose.yml file join this network automatically—no manual configuration needed.
Think of it like a private neighborhood: every house (container) on the street can easily find and talk to its neighbors without needing full street addresses.
Service Discovery by Name
Here's the magic: containers can reach each other using their service names as hostnames. If you have a service called web and another called database, the web container can connect to database:5432 directly.
services:
web:
image: nginx
# Can connect to http://api:8080 internally
api:
image: myapi
# Can connect to database:5432
database:
image: postgres
No IP addresses, no hardcoding—just clean service names.
Custom Networks
You can define your own networks for more control, like isolating groups of services:
services:
web:
networks:
- frontend
api:
networks:
- frontend
- backend
database:
networks:
- backend
networks:
frontend:
backend:
Now web can reach api, and api can reach database, but web and database are isolated from each other—just like separate VLANs.
Why This Matters
This automatic networking eliminates the need to manage container IPs manually and makes your applications portable. The same Compose file works anywhere, with services discovering each other seamlessly.
Key Takeaway: Docker Compose creates isolated networks automatically, enables service discovery by name, and lets you define custom networks for segmentation—all making container communication simple and maintainable.