Advanced15 min read
Docker Compose Stacks
Deploy multi-service Docker Compose stacks with service discovery, networks, volumes, and health checks.
1.Compose File Structure
Define multi-service applications with Docker Compose.
- Create docker-compose.yml in your repository root
- Define services: web, api, db, redis, worker, etc.
- Use depends_on for startup order
- Configure networks for service isolation
- Define volumes for persistent data
# docker-compose.yml
version: '3.8'
services:
web:
build: .
ports: ["3000:3000"]
depends_on: [db, redis]
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/app
- REDIS_URL=redis://redis:6379
api:
build: ./api
depends_on: [db]
db:
image: postgres:15
environment:
POSTGRES_DB: app
POSTGRES_USER: user
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes: [db_data:/var/lib/postgresql/data]
redis:
image: redis:7-alpine
volumes:
db_data:2.Service Configuration
Configure each service for production.
- Set restart: unless-stopped for auto-recovery
- Define health checks: healthcheck: test: [...], interval: 30s
- Limit resources: deploy: resources: limits: cpus: '0.5', memory: 512M
- Use secrets for sensitive config: secrets: [db_password]
- Set logging driver: logging: driver: json-file, options: max-size: 10m
3.Networks and Volumes
Isolate services and persist data.
- Frontend network: public-facing services (web, api gateway)
- Backend network: internal services (db, redis, workers)
- Named volumes for database, redis, uploads
- Bind mounts for development (code hot-reload)
- Volume backup: include in Oxaploy backup policy
4.Health Checks and Dependencies
Ensure services start in correct order and recover from failures.
- Database: healthcheck: test: ['CMD-SHELL', 'pg_isready -U postgres'], interval: 10s
- Redis: healthcheck: test: ['CMD', 'redis-cli', 'ping'], interval: 10s
- Web: healthcheck: test: ['CMD', 'curl', '-f', 'http://localhost:3000/healthz'], interval: 30s
- Use depends_on: condition: service_healthy for ordered startup
- Oxaploy monitors health checks and restarts unhealthy containers