Skip to content
Docker

Docker Compose

Define and run multi-container apps with compose.

By EZ4Code Team
composeorchestrationyaml

Code

# docker-compose.yml
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
    depends_on:
      - api
  api:
    build: ./api
    environment:
      - DB_HOST=db
    depends_on:
      - db
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: secret
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

# Commands
# docker compose up -d        # start in background
# docker compose logs -f web   # follow logs
# docker compose down          # stop and remove

Explanation

Compose lets you declare multiple services, their dependencies, networks, and volumes in a single YAML file. depends_on controls startup order but does not wait for readiness; use healthchecks for that. docker compose up brings the stack up, and down tears it down.

More Docker Snippets