Docker Compose Intro

View saved

Why Compose

Real apps often need more than one process—a web server and a database, or nginx in front of an app. Compose defines those services together and starts them with one command.

Sketch a web + database file

Save this as compose.yaml next to your project. Passwords here are for local learning only.

services:
  web:
    image: nginx:1.27
    ports:
      - "8080:80"
    volumes:
      - ./site:/usr/share/nginx/html:ro
    depends_on:
      - db
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: learning
      POSTGRES_DB: app
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

Start and stop the stack

From the folder that contains compose.yaml:

docker compose up -d
docker compose ps
docker compose down

Keep data when tearing down

docker compose down stops and removes containers and the default network. Add -v only when you intentionally want to delete named volumes too.

docker compose down -v

Comments

One comment per signed-in account. Comments are saved with this page’s URL.