The Compose file: services, networks and volumes
How compose.yaml is laid out: services, networks and volumes, short and long syntax, isolated networks, external resources, and reuse with x- blocks.
The Compose file: services, networks and volumes
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A Compose file is a map of top-level elements. `services` is required. `networks`, `volumes`, `configs` and `secrets` declare shared resources that services refer to by name.
- Each service is one container definition: `image` or `build`, plus the options you would otherwise pass to `docker run`.
- Many attributes have a short string form and a long mapping form. Compose expands the short form, and `docker compose config` shows the expanded result.
- Services on the same network reach each other by service name. Split networks to limit who can talk to whom, and mark a back-end network `internal: true` to cut it off from outside.
- `ports` publishes on the host, on every interface unless you prefix an IP such as 127.0.0.1. `expose` only documents a port for other containers and publishes nothing.
- Named volumes and networks are created and prefixed by Compose. Mark one `external: true` when it is managed elsewhere and must already exist.
- Top-level keys starting with `x-` are ignored by Compose, so they can hold YAML anchors that several services merge with `<<:`.
A file is a map of resources
Why Compose exists showed a file that replaced a page of docker run commands. This lesson reads one properly. A Compose file is a YAML map. Its top-level keys are the kinds of thing a project owns:
| Key | What it declares |
|---|---|
services |
The containers to run. Required. |
networks |
Networks services attach to. A default one exists even if you declare none. |
volumes |
Named volumes services mount. |
configs, secrets |
Files mounted into containers. Secrets get their own lesson. |
name |
The project name, instead of the directory name. |
include |
Other Compose files to pull in. Covered under development. |
Recent Compose versions add jobs for one-shot and scheduled work, which Docker still marks experimental, and models for AI models. You can ignore both until you need them.
The services refer to the top-level resources by name. Think of a building plan: the top level lists the shared corridors and storerooms once, and each room’s entry says which corridors it opens onto.
Services
Each entry under services is one container definition. It either names an image to pull or a build section to make one, plus the options you would pass to docker run:
services:
api:
build: ./api # build from ./api/Dockerfile
command: ["gunicorn", "shop.wsgi", "-b", "0.0.0.0:8000"]
restart: unless-stopped
environment:
DATABASE_URL: postgres://shop@db/shop
networks: [frontend, backend]
The reference splits the attributes into three pages: the container specification (image, environment, volumes, user, resource limits, and so on), the workload specification (build, depends_on, healthcheck, ports), and service-only attributes such as restart, profiles and deploy. You don’t need to memorise them. You need to know that almost any docker run flag has a key here.
Short syntax and long syntax
Many attributes accept a compact string or a full mapping. "80:80" is short for a port mapping with a target, a published port and a protocol. pgdata:/var/lib/postgresql is short for a volume mount with a type, source and target. Compose expands every short form before doing anything.
To see that expansion, I wrote a three-service file, proxy on a frontend network, db on a backend network and api on both, and loaded it with compose-go, the library the Compose CLI uses. The output is what docker compose config prints. Abridged:
name: shop
services:
db:
image: postgres:18
networks:
backend: null
volumes:
- type: volume
source: pgdata
target: /var/lib/postgresql
proxy:
image: nginx:1.31
networks:
frontend: null
ports:
- mode: ingress
target: 80
published: "80"
protocol: tcp
networks:
backend:
name: shop_backend
internal: true
frontend:
name: shop_frontend
volumes:
pgdata:
name: shop_pgdata
Two habits come from this. When a file misbehaves, run docker compose config and read what Compose actually understood, not what you meant. And reach for the long form when you need an option the short form can’t express, such as read_only: true on a bind mount or host_ip on a port.
Networks: who can talk to whom
Services on the same network reach each other by service name through Docker’s embedded DNS, as in user-defined networks. If you declare no networks, every service joins the project’s default network and everything can reach everything.
Splitting networks is a cheap security boundary:
networks:
frontend:
backend:
internal: true
The proxy can reach the API. The API can reach the database. The proxy cannot reach the database at all, because they share no network. internal: true goes further and gives backend no route out, so a compromised database container cannot download a payload or phone home.
Ports deserve care. ports publishes on the host, and with no host IP it binds every interface. "127.0.0.1:5432:5432" keeps a dev database on loopback. How published ports interact with the host firewall is its own lesson: publishing ports and the host firewall. expose publishes nothing. It only documents a port for readers, since containers on a shared network can connect to any listening port anyway.
Volumes and external resources
A volume declared at the top level is created on first up with the project prefix, shop_pgdata here, and survives docker compose down. Its full behaviour is in named volumes. Bind mounts, host paths such as ./nginx.conf:/etc/nginx/nginx.conf:ro, need no top-level entry.
Sometimes the resource belongs to someone else. A shared reverse-proxy network created by another project, or a volume restored from a backup:
networks:
proxy:
external: true # must already exist; Compose won't create or delete it
volumes:
pgdata:
external: true
name: shop_pgdata_restored
Compose then neither creates nor removes it, and errors if it is missing.
Don’t repeat yourself: x- blocks and anchors
A web process and a worker often share an image, environment and restart policy. Compose ignores any top-level key starting with x-, so it can hold a YAML anchor:
x-app: &app
image: registry.example.com/shop:1.4.2
restart: unless-stopped
environment:
DATABASE_URL: postgres://shop@db/shop
services:
web:
<<: *app
command: ["gunicorn", "shop.wsgi"]
ports:
- "127.0.0.1:8000:8000"
worker:
<<: *app
command: ["celery", "-A", "shop", "worker"]
Loading this with compose-go gave both services the image, restart policy and environment, each with its own command, and the web port with host_ip: 127.0.0.1. The anchor is plain YAML, resolved before Compose sees the file.
About the video
The embed is all of “Understanding Docker Compose Files, for New Users” by Hardwood Homelab, published October 2026, 20 minutes. The creator takes the most complicated Compose file he has come across and explains each part. The chapters cover the file structure, top-level volumes and networks, then services, image, restart, storage, depends_on, ports, environment, env files and command. That is this lesson’s ground, from a self-hosting angle. It is a small channel, and I judged it from its chapters and description rather than a transcript. It doesn’t show the long syntax expansion or internal networks, which the lesson adds. English is confirmed by YouTube’s “English (US) original” audio flag and an English caption track. Mosh’s 2022 tutorial in deeper builds a file from scratch, but uses the older version: key.
What was run: the example files were loaded with a small Go program built on compose-go v2.17.0, the library Compose v5.8.1 uses, and the printed models are its real output. No daemon was available, so nothing was started. Network and port behaviour is from the Compose file reference, checked 2026-11-28.
Where this fits
Your services now find each other. They don’t yet wait for each other: startup order, depends_on and health checks.
Check yourself
The db service is on a network named backend with internal: true, and the api service is on both backend and frontend. Can api reach db, and can db reach the internet?
api can reach db, because they share backend. db cannot reach the internet, because an internal network has no external connectivity.
What is the difference between ports and expose on a service?
ports publishes a container port on the host, so outside clients can connect. expose only records a port for other containers on the same network, which can connect to any port anyway, and publishes nothing.
You only want a database reachable from your laptop, not from the office network. How do you write the port?
Bind it to loopback: "127.0.0.1:5432:5432". Without a host IP the port is published on every interface.
Your compose.yaml references a volume that another team's project created. How do you stop Compose from creating a new, empty, prefixed volume?
Declare it at the top level with external: true (and name: if its real name differs). Compose then uses the existing volume and errors if it doesn't exist.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.