9.1.1.2 Compose Shared Networks
A focused guide to Compose Shared Networks, connecting core concepts with practical Docker and container operations.
Compose shared networks are the network or networks Compose automatically creates (or that can be explicitly defined) connecting an application's services together, providing the same name-based resolution and connectivity a manually created user-defined Docker network would, but configured automatically based on the Compose file's structure.
The Automatic Default Network
Without any explicit network configuration, Compose creates a single default network for the entire application, attaching every defined service to it automatically.
services:
api:
build: .
db:
image: postgres:16
docker compose up -d
docker compose exec api ping db
This succeeds without any explicit network configuration in the Compose file at all, since Compose handled creating and attaching both services to a shared network automatically.
Defining Multiple, Explicit Networks
For applications needing more deliberate network segmentation — isolating a backend tier from a frontend tier, for instance — explicit networks can be defined and assigned to specific services.
services:
frontend:
networks:
- frontend-net
backend:
networks:
- frontend-net
- backend-net
database:
networks:
- backend-net
networks:
frontend-net:
backend-net:
Here, frontend cannot directly reach database, since they don't share a common network — only backend, attached to both, can communicate with each.
Why Compose Networking Builds on Standard Docker Networking
Underlying this automatic behavior is the same user-defined network mechanism available outside of Compose entirely — Compose simply automates the creation and service attachment that would otherwise require manual docker network commands.
docker network ls
This reveals the actual underlying Docker network Compose created and is managing on the application's behalf.
Why Compose Shared Networks Matter
Compose's automatic network handling removes a significant amount of manual setup that would otherwise be required for reliable multi-container communication, while still allowing more deliberate, explicit network segmentation when an application's architecture genuinely calls for it.