Sub topic 9
Networking with Docker Compose
This chapter assumes you can run containers with docker run, know what a user-defined bridge network is, and can read YAML. Compose builds on the features from earlier chapters: DNS-based name resolution, published ports, and network isolation. It lets you declare them in one file.
What Compose creates by default
When you run docker compose up, Compose creates one user-defined bridge network for the project. The network is named <project>_default, where the project name is the directory name unless you set it. Every service joins this network.
Two consequences follow:
- Each service is reachable from the others by its service name, through Docker's embedded DNS.
- Nothing is reachable from the host unless you publish a port with
ports.
A service reaches another service on the container port, not the published one. If db listens on 5432, other services connect to db:5432, whether or not 5432 is published.
Segmenting with multiple networks
You can define your own networks and attach each service to the ones it needs. A service can only talk to services that share at least one network with it.
services:
proxy:
image: nginx:alpine
ports:
- "8080:80"
networks:
- frontend
- backend
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
networks:
- backend
networks:
frontend:
backend:
internal: trueHere proxy is on both networks and publishes port 8080. db is only on backend. Setting internal: true on backend removes external access for that network, so containers on it cannot reach the internet and the host cannot reach them through published ports.
Note that proxy publishes its port while also being on the internal network. Publishing works because proxy also sits on frontend, which is not internal.
Checking the result
Start the project and list its networks:
docker compose up -d
docker network ls
docker compose exec proxy getent hosts dbYou should see networks named after your project, such as myproj_frontend and myproj_backend. The getent command shows db resolving to an address on the backend network.
Useful network options
| Option | Where | Effect |
|---|---|---|
| `aliases` | service network entry | Adds extra DNS names for a service on that network |
| `ipv4_address` | service network entry | Assigns a fixed address (needs a defined `subnet`) |
| `internal: true` | network | Blocks external access |
| `external: true` | network | Uses a network that already exists instead of creating one |
| `name` | network | Sets the exact network name, without the project prefix |
Aliases use the long syntax:
services:
api:
image: nginx:alpine
networks:
backend:
aliases:
- api.internal
networks:
backend:Common errors
- Connecting to the published port from another container. Use
db:5432, notdb:15432. Published ports are for the host and outside clients. - Using
localhostbetween services. Inside a container,localhostis the container itself. Use the service name. - A service on only an internal network that needs the internet. It will fail to pull updates or call external APIs. Add it to a non-internal network as well.
- Referencing a network that is not declared. Compose rejects a service that lists an undefined network under
networks. - Port conflicts. Two services publishing the same host port will fail to start the second one.
Exercise
A Compose file defines web on networks frontend and backend, and db only on backend (marked internal: true). Which hostname and port should web use to reach a Postgres server in db, and can db download files from the internet?
Show answer
web connects to db:5432, using the service name and the container port. db cannot reach the internet, because its only network is internal.