SyntaxStudy
Sign Up
Docker Beginner 12 min read

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. With a single docker-compose.yml file you configure all your services, networks, and volumes, then start everything with one command.

Example
# docker-compose.yml
version: '3.8'

services:
  # Web application
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - DB_HOST=db
      - REDIS_HOST=cache
    depends_on:
      - db
      - cache
    volumes:
      - ./logs:/app/logs

  # Database
  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data

  # Cache
  cache:
    image: redis:7-alpine
    ports:
      - "6379:6379"

volumes:
  postgres_data:

# Commands:
# docker compose up -d       # start all services
# docker compose down        # stop all services
# docker compose logs -f app # follow logs
# docker compose exec app sh # shell into container