Course outline · 0% complete

0/29 lessons0%

Course overview →

One file for the whole app

lesson 5-1 · ~12 min · 14/29

Quiz

Warm-up from unit 4: to boot the two-container app you needed docker network create, a db run with a volume, and an app run with ports. Roughly how many long commands, in the right order, every single time?

docker compose

Compose lets you describe your whole multi-container setup in one file, docker-compose.yml, and boot it with one command:

docker compose up -d

The file is written in YAML, a config format where structure comes from indentation and most lines are key: value. Two spaces per level, no tabs. You will read YAML constantly from here on, since GitHub Actions in unit 7 uses it too.

Compose reads the file, then creates the network, the volumes, and every container, in the right order, with the right flags. Delete everything just as easily with docker compose down. The file lives in your repo, so the entire runnable app setup is version-controlled with git, like everything else.

The file, decoded line by line

services:
  app:
    build: .
    ports:
      - "8080:8000"
    environment:
      DATABASE_URL: postgres://shop:secret@db:5432/shop
    depends_on:
      - db
  db:
    image: postgres:16
    volumes:
      - dbdata:/var/lib/postgresql/data
volumes:
  dbdata:
  • services:: the containers to run. Each key under it (app, db) is one service, and its name becomes the container's network hostname, exactly like --name in lesson 4-2
  • build: .: build this service's image from the Dockerfile in the current folder
  • ports: - "8080:8000": the -p 8080:8000 flag from lesson 2-2, as YAML
  • environment:: environment variables handed to the container
  • depends_on: - db: start db before app
  • image: postgres:16: use a prebuilt image instead of building one
  • volumes: - dbdata:...: the -v flag from lesson 4-1, and the top-level volumes: block declares the named volume

Compose also creates a private network for these services automatically. No docker network create needed.

Quiz

In the compose file above, the app's DATABASE_URL points at host `db`. Why does that hostname work?

Quiz

In the compose file, the app service says `build: .` while the db service says `image: postgres:16`. What is the difference?

Problem

What is the conventional filename that docker compose looks for in your project folder?