Docker Fundamentals

Lesson 5 of 6

Docker Compose: Multi-Container Apps

Real applications are rarely a single container, you usually need an app server, a database, maybe a cache. Docker Compose describes a whole multi-container setup in one file.

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgresql://user:pass@db:5432/app
    depends_on:
      - db

  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: pass
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:

Running it

docker compose up -d
docker compose down

docker compose up builds (if needed) and starts every service defined in the file. Notice web reaches the database at the hostname db, Compose automatically creates a shared network where each service can find the others by service name.

depends_on controls startup order, but doesn't wait for the database to be fully ready to accept connections, that's a common real-world gotcha worth knowing about early.

📝 Compose Quiz

Passing score: 70%
  1. 1.In Docker Compose, how does one service reach another (e.g. web reaching db)?

  2. 2.docker compose ____ builds and starts every service defined in the compose file.

  3. 3.depends_on guarantees a service won't start until the service it depends on is fully ready to accept connections.