Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

DK
Docker
0/10 completed
Beginner
Why containers?Install Docker and run your first containerImages vs containers
Intermediate
Writing a DockerfileVolumes and persistent dataNetworking and ports
Advanced
Docker ComposeMulti-stage builds and small images
Pro
Security and production best practicesRegistries and CI/CD
Tracks / Docker / Advanced

Docker Compose

Docker Compose describes a multi-container app in one YAML file (compose.yaml) and runs it with one command. It creates the networks and volumes and starts everything in the right order. It's the standard way to run apps locally.

A full-stack app in one file

Let's run a Node API with PostgreSQL and Redis:

yaml
# compose.yaml
services:
  api:
    build: .                       # build from ./Dockerfile
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgres://app:secret@db:5432/appdb
      REDIS_URL: redis://cache:6379
    depends_on:
      db:
        condition: service_healthy # wait until Postgres is ready
      cache:
        condition: service_started
    restart: unless-stopped

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: appdb
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app -d appdb"]
      interval: 5s
      timeout: 3s
      retries: 10

  cache:
    image: redis:7-alpine

volumes:
  pgdata:
  • Every service becomes a container. Service names (db, cache) are their hostnames.
  • Compose automatically creates a private network for the project.
  • depends_on with service_healthy waits for the health check to pass, not just for the container to start.
  • Only api publishes a port. The database stays private.

Everyday commands

bash
docker compose up -d            # build (if needed) and start everything in the background
docker compose ps               # status
docker compose logs -f api      # follow one service's logs
docker compose exec db psql -U app appdb   # shell into a service
docker compose restart api
docker compose up -d --build    # rebuild after code changes
docker compose down             # stop and remove containers + network
docker compose down -v          # …and delete volumes (wipes the database!)

Environment variables and .env

Don't hard-code secrets in compose.yaml. Compose automatically reads a .env file in the same folder:

bash
# .env  (add to .gitignore!)
POSTGRES_PASSWORD=super-secret
API_PORT=3000
yaml
services:
  db:
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
  api:
    ports:
      - "${API_PORT:-3000}:3000"     # default value if unset
    env_file:
      - ./api.env                    # load many variables from a file

Development setup: live reload

Use a second file to layer dev-only settings on top:

yaml
# compose.override.yaml — loaded automatically by `docker compose up`
services:
  api:
    command: npx nodemon server.js
    volumes:
      - ./:/app
      - /app/node_modules
    environment:
      NODE_ENV: development

For production, use explicit files: docker compose -f compose.yaml -f compose.prod.yaml up -d.

Newer Compose versions also support develop: watch:, which syncs files and rebuilds automatically with docker compose watch.

Scaling and profiles

bash
docker compose up -d --scale worker=3     # run 3 copies of a stateless service
yaml
services:
  adminer:                  # DB admin UI, only when asked for
    image: adminer
    ports: ["8081:8080"]
    profiles: ["tools"]
bash
docker compose --profile tools up -d

Compose vs Kubernetes

Compose is perfect for local development, CI test environments and small single-server deployments. When you need many servers, self-healing, rolling updates and autoscaling, you graduate to Kubernetes. The concepts (services, volumes, env, health checks) carry over directly.

Practice

Write a compose.yaml for WordPress + MySQL:

  • db: mysql:8 with a named volume and a health check (mysqladmin ping)
  • wordpress: the wordpress image on port 8080, connecting to db, starting after the DB is healthy
  • Passwords come from a .env file

Run it, finish the WordPress installer at http://localhost:8080, then down and up again and confirm your site survived.

✎ Check your understanding

1.In Compose, how does the api service connect to the db service?

2.What does docker compose down -v do that down doesn't?

← Networking and ports