Sikau

DevOps and deployment · Day 9 · about 40 min

Docker Compose

Describe a multi-container app in compose.yaml and run it with one command.

One file, whole stack

Typing long docker run commands for the API, database and cache gets old fast. Docker Compose describes all services, their networks and volumes in compose.yaml; docker compose up -d starts everything, down removes it.

compose.yaml

services:
  api:
    build: .
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: production
      DB_HOST: db
    depends_on:
      - db
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:
  • docker compose up -d --build rebuilds images.
  • docker compose logs -f api, exec db psql ….
  • down keeps volumes; down -v deletes them too.

The rest of this lesson

  • A full compose.yamlexplain
  • Everyday Compose commandsexplain
  • What's the hostname?predict
  • Compose commandmatch
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 14 lessons of DevOps and deployment