Sub topic 5
Service networking, DNS names, and published ports
This chapter assumes you can read a compose.yaml, know what a container and an image are, and have run docker compose up before. It covers how services find each other and how traffic from your machine reaches them.
The default network
When you run docker compose up, Compose creates one bridge network for the project. It is named <project>_default, so the stack named shop gets shop_default. Every service joins it unless you configure otherwise.
Compose registers each service name as a DNS name on that network. In the example stack, web connects to the database with the hostname db:
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/appDocker's embedded DNS resolves db to the IP address of the database container. If the container is recreated and gets a new address, the name still works. Never hard-code container IP addresses.
Published ports and container ports
Two different port numbers are involved, and mixing them up is the most common networking error.
- The container port is where the process listens inside the container.
- The published port is the port on the host that Docker forwards to the container port.
The ports entry uses the form "HOST:CONTAINER":
services:
web:
build: .
ports:
- "8080:8000"Here a browser on your machine uses localhost:8080, and Docker forwards the traffic to port 8000 in the container. Quote port mappings. YAML can read unquoted values such as 22:22 as a base-60 number.
Services talking to each other do not use published ports. They use the container port directly. In the example, web reaches the database at db:5432, even if the host publishes db on a different port such as 5433:5432.
| Who connects | Address to use | Uses `ports`? |
|---|---|---|
| Browser on the host | `localhost:8080` | Yes |
| Another service in the stack | `web:8000` | No |
| Service to database | `db:5432` | No |
Keep internal services private
The db service in the example has no ports entry. Other services can reach it, but nothing outside Docker can. This is the safer default. Publish a port only for services that a person or an outside system must reach. If you publish a database for a local GUI client, bind it to the loopback address so other machines cannot connect:
ports:
- "127.0.0.1:5433:5432"If a service only needs to document its port for other tools, expose records it without publishing it. It does not change connectivity, because containers on the same network can already reach each other's ports.
Custom networks
You can define networks yourself to separate groups of services:
services:
web:
build: .
networks: [frontend, backend]
db:
image: postgres:16
networks: [backend]
proxy:
image: nginx
networks: [frontend]
ports:
- "80:80"
networks:
frontend:
backend:proxy can reach web, and web can reach db. proxy cannot reach db, because they share no network. Once a service lists networks, it no longer joins the default network.
Common errors
- Using
localhostbetween containers. Insideweb,localhostis the web container itself. Use the service name. - Listening on 127.0.0.1 inside the container. A published port only works if the process listens on
0.0.0.0. Many frameworks default to loopback in development. - Host port already in use. Two stacks publishing
8000conflict. Change the host side of the mapping. - Connection refused at startup. The name resolves, but the process is not ready. This is a startup order problem, covered in the health checks chapter.
Exercise
A stack publishes ports: ["5433:5432"] on the db service. A web service in the same stack needs to connect. Which host and port should its connection string use, and why?
Show answer
Use db:5432. Services on the same Compose network resolve the service name through Docker's DNS and connect to the container port. The published port 5433 only applies to connections from the host machine.