Skip
Arish's avatar

22. Introduction to Docker Compose


What is Docker Compose?

Tool for defining multi-container applications in YAML.

Basic docker-compose.yml

yaml
1version: '3.8'
2
3services:
4  web:
5    build: .
6    ports:
7      - "3000:3000"
8    depends_on:
9      - db
10      - redis
11    environment:
12      - DATABASE_URL=postgres://postgres:secret@db/myapp
13      - REDIS_URL=redis://redis:6379
14
15  db:
16    image: postgres:15
17    environment:
18      - POSTGRES_PASSWORD=secret
19    volumes:
20      - postgres-data:/var/lib/postgresql/data
21
22  redis:
23    image: redis:7
24
25volumes:
26  postgres-data:

Basic Commands

bash
1# Start services
2docker compose up
3
4# Start in background
5docker compose up -d
6
7# Stop services
8docker compose down
9
10# View logs
11docker compose logs -f
12
13# Rebuild
14docker compose build
15docker compose up --build

Benefits

  • Single command to start entire stack
  • Automatic networking between services
  • Volume and environment management
  • Reproducible environments