Linux Labs

Containers: Docker and compose / Stage 8 of 11

docker compose: a stack in one file

Not running Starting usually ~20 s
Leave the lesson

Step 4 of 7 · theory · 4 min

Volumes and down

A stack is taken down with one command. Here: what stays after it.

A service connects a named volume by name, and the volume itself is declared at the top level of the file — with the volumes: key next to services:. compose prefixes the volume name with the project name: the volume pgdata of the project shop is called shop_pgdata.

yaml
services:
  db:
    image: busybox
    volumes:
      - pgdata:/data     # a project volume
volumes:
  pgdata:

docker compose down stops and removes the project's containers and its network. The volumes stay: the data outlives re-creating the stack. To remove them along with the stack — docker compose down -v.

Check yourself 1 question

~/notes/compose.yaml declares the volume notes-data. The stack was taken down with sudo docker compose down. What will sudo docker volume ls show?

  • compose prefixes the volume name with the project name, the notes directory: the volume is called notes_notes-data.

  • notes_default is the project's network, and down removes it. sudo docker network ls lists networks, sudo docker volume ls lists volumes.

  • down removes the project's containers and network but keeps the volumes. docker compose down -v removes the stack together with its volumes.

down removes the project's containers and network and keeps its volumes, so the data outlives re-creating the stack. compose built the volume name from the project name and the name in the file: notes_notes-data.

Key points

  • A named volume is declared with the top-level volumes: key and is called <project>_<volume>.
  • down removes the project's containers and network; the volumes stay.
  • down -v removes the volumes along with the stack.
Ubuntu 24.04
Loading…