1. Compose Profiles
Profiles cho phép bạn chọn services nào sẽ được start, phù hợp cho multiple environments:
services:
api:
image: myapp:latest
# Không có profile → luôn start
db:
image: postgres:16
# Không có profile → luôn start
adminer:
image: adminer
profiles:
- debug
ports:
- "8080:8080"
prometheus:
image: prom/prometheus
profiles:
- monitoring
grafana:
image: grafana/grafana
profiles:
- monitoring
# Start default services (api, db)
docker compose up -d
# Start với debug profile
docker compose --profile debug up -d
# Start với monitoring profile
docker compose --profile monitoring up -d
# Start với nhiều profiles
docker compose --profile debug --profile monitoring up -d
2. Override Files
Docker Compose tự động merge docker-compose.yml với docker-compose.override.yml:
# docker-compose.yml (base)
services:
api:
build: .
ports:
- "3000:3000"
environment:
NODE_ENV: production
# docker-compose.override.yml (development - auto-loaded)
services:
api:
volumes:
- ./src:/app/src
environment:
NODE_ENV: development
DEBUG: "true"
command: npm run dev
# docker-compose.prod.yml (production - manual)
services:
api:
image: registry.example.com/myapp:latest
deploy:
replicas: 3
resources:
limits:
cpus: '1.0'
memory: 512M
restart: always
# Development (auto-loads override)
docker compose up -d
# Production (explicit files)
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d
3. Variable Substitution
# docker-compose.yml
services:
api:
image: ${REGISTRY:-docker.io}/${IMAGE_NAME:-myapp}:${TAG:-latest}
ports:
- "${API_PORT:-3000}:3000"
environment:
DB_HOST: ${DB_HOST:-db}
DB_PASSWORD: ${DB_PASSWORD:?Database password is required}
# .env file (auto-loaded)
REGISTRY=harbor.example.com
IMAGE_NAME=myapp
TAG=v1.2.3
API_PORT=8080
DB_HOST=db
DB_PASSWORD=mysecretpassword
Syntax:
${VAR}- Sử dụng biến, lỗi nếu không set${VAR:-default}- Default value nếu không set hoặc empty${VAR-default}- Default value nếu không set${VAR:?error}- Lỗi với message nếu không set
4. Deploy Configuration
services:
api:
image: myapp:latest
deploy:
replicas: 3
resources:
limits:
cpus: '0.50'
memory: 256M
reservations:
cpus: '0.25'
memory: 128M
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120s
update_config:
parallelism: 1
delay: 10s
order: start-first
rollback_config:
parallelism: 1
delay: 5s
5. Extends
# common.yml
services:
base-app:
build: .
environment:
LOG_LEVEL: info
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
# docker-compose.yml
services:
api:
extends:
file: common.yml
service: base-app
ports:
- "3000:3000"
environment:
SERVICE_NAME: api
worker:
extends:
file: common.yml
service: base-app
command: npm run worker
environment:
SERVICE_NAME: worker
6. Compose Watch (Hot Reload)
Compose Watch cho phép tự động sync files và rebuild khi code thay đổi:
services: api: build: . develop: watch: # Sync source code (hot reload) - action: sync path: ./src target: /app/src# Rebuild khi dependencies thay đổi - action: rebuild path: ./package.json # Sync + restart khi config thay đổi - action: sync+restart path: ./config target: /app/config
# Start với watch mode
docker compose watch
# Hoặc
docker compose up --watch
7. Scaling Services
# Scale service docker compose up -d --scale api=3Hoặc trong compose file
services:
api:
deploy:
replicas: 3
# Load balancing với Nginx
services:
nginx:
image: nginx:1.27-alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- api
api:
build: .
deploy:
replicas: 3
# Không expose ports ra host, chỉ qua nginx
8. Production-ready Compose Configuration
# docker-compose.prod.yml
services:
api:
image: harbor.example.com/myapp:${TAG}
deploy:
replicas: 2
resources:
limits:
cpus: '1.0'
memory: 512M
environment:
NODE_ENV: production
healthcheck:
test: ["CMD", "wget", "--spider", "-q", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
restart: unless-stopped
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
read_only: true
tmpfs:
- /tmp
security_opt:
- no-new-privileges:true
9. Tổng kết
Trong bài này, bạn đã nắm được:
Compose Profiles cho multiple environments
Override files cho dev/staging/production
Variable substitution với .env files
Deploy configuration và resource limits
Extends để tái sử dụng common configuration
Compose Watch cho hot-reload development
Scaling services và production-ready configurations
Bài tiếp theo sẽ hướng dẫn quản lý Environment Variables, Secrets và Configuration.