DOCKER - 6 2 Docker Compose YAML file
This lesson walks through a docker-compose.yml template in VS Code so you know what the file is supposed to look like. The first line declares the Compose file version — for example version: "3.1". If you omit it, Compose assumes version 1, which lacks most modern features — always include the version. The three main top-level sections that follow are services, volumes and networks.
Anatomy of a compose file
YAML is whitespace-sensitive: indentation can be 2 or 4 spaces, but it must be consistent. A modern editor will tell you when it isn't. Under services you list one entry per container — Compose calls them services because each entry can be scaled to several identical containers. The service name (any string you like) becomes the DNS name used by other containers on the project's network, just like --name on docker container run.
Inside a service, you can specify the image, an alternative command, ports, volumes, environment variables and so on — basically everything you'd otherwise pass to docker container run. That's the whole point of Compose: stop typing long ad-hoc commands or maintaining brittle shell scripts. The file is readable, documented, and version-controlled.
version: "3.1"
services:
jekyll:
image: bretfisher/jekyll-serve
volumes:
- .:/site
ports:
- "80:4000"
Notice . (the project directory) replaces the host path you would have passed via $(pwd) on the CLI. Lists in YAML use the - prefix and plural keys (volumes, ports); single-value entries use plain key/value pairs (image).
- services — one or more named containers; the name becomes a DNS hostname inside the project network
- volumes — top-level section if you want named volumes
- networks — optional; Compose creates a default network anyway
- environment — key/value form (no dash) or list form (with dash)
A more involved example chains a MySQL container and a WordPress container, passing each service its config through environment. Anything you would write on the command line goes into Compose. Full documentation lives at docs.docker.com/compose — every key is listed with multiple examples. The next lesson turns to the Compose CLI itself.