Networking in Compose
Imagine a city blueprint where each building (service) is connected by roads (networks). Without roads, the hospital can’t reach the school, and the power station can’t deliver electricity. In Docker Compose, networks are those roads - they connect services together, enabling communication and collaboration.
Networks Foundations
1. Networking in Compose
- By default, Compose creates a default network for all services in a project.
- Services in the same Compose file can communicate using service names as hostnames.
- Networks can be customized for isolation and control.
2. Types of Networks in Compose
- Default Network:
- Automatically created when you run
docker-compose up. - All services are attached unless specified otherwise.
- Automatically created when you run
- Custom Networks:
- Defined explicitly in the Compose file.
- Useful for isolating groups of services.
- External Networks:
- Connect services to pre‑existing Docker networks.
3. Service Communication
- Services resolve each other by service name.
- Example:
webcan connect todbusingdb:3306.
- No need to hardcode IP addresses.
4. Defining Networks in Compose
version: '3'
services:
web:
image: nginx
networks:
- appnet
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=secret
networks:
- appnet
networks:
appnet:
5. Multiple Networks Example
version: '3'
services:
frontend:
image: nginx
networks:
- publicnet
- privatenet
backend:
image: redis
networks:
- privatenet
networks:
publicnet:
privatenet:
frontendcan talk to bothbackendand external clients.backendis isolated inprivatenet.
Things to Remember
- Compose automatically creates a default network.
- Service names act as DNS hostnames.
- Custom networks provide isolation and flexibility.
- Multiple networks allow fine‑grained control of communication.
Hands‑On Lab
Step 1: Create a Compose File with a Custom Network
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
networks:
- appnet
db:
image: redis
networks:
- appnet
networks:
appnet:
Step 2: Run the Stack
docker-compose up -d
Step 3: Test Communication
docker exec -it web ping db
webresolvesdbby name.
Step 4: Inspect Networks
docker network ls
docker network inspect appnet
Practice Exercise
- Create a Compose file with three services:
frontend(nginx) exposed on port 8081.backend(node.js app).database(mysql).
- Define two networks:
publicnetandprivatenet. - Attach
frontendto both networks,backendanddatabaseonly toprivatenet. - Verify that
frontendcan talk tobackendanddatabase, but external clients only reachfrontend.
Visual Learning Model
Networks in Compose
├── publicnet
│ └── frontend (nginx)
└── privatenet
├── frontend (nginx)
├── backend (node.js)
└── database (mysql)
The Hackers Notebook
Networking in Compose connects services together using default or custom networks. Service names act as DNS hostnames, eliminating the need for IP addresses. With multiple networks, developers can isolate services while still allowing controlled communication, making Compose ideal for microservice architectures.

Updated on Dec 26, 2025