-
-
Notifications
You must be signed in to change notification settings - Fork 0
Docker Compose Reference
Norm Brandinger edited this page Nov 20, 2025
·
1 revision
- Service Definitions
- Dependencies and Health Checks
- Volume Mounts
- Network Configuration
- Environment Variables
- Custom Modifications
Basic service structure:
services:
postgres:
image: postgres:16-alpine
container_name: dev-postgres
hostname: postgres
entrypoint: ["/init/init.sh"]
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
VAULT_ADDR: http://vault:8200
VAULT_TOKEN: ${VAULT_TOKEN}
volumes:
- postgres-data:/var/lib/postgresql/data
- ./configs/postgres/scripts/init.sh:/init/init.sh:ro
ports:
- "${POSTGRES_PORT}:5432"
networks:
dev-services:
ipv4_address: ${POSTGRES_IP}
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
depends_on:
vault:
condition: service_healthyAll services depend on Vault:
depends_on:
vault:
condition: service_healthyHealth check examples:
# PostgreSQL
healthcheck:
test: ["CMD-SHELL", "pg_isready -U devuser"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
# Vault
healthcheck:
test: ["CMD", "vault", "status"]
interval: 10s
# Redis
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
# HTTP service
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 30sData volumes:
volumes:
postgres-data:
driver: local
mysql-data:
mongodb-data:
redis-1-data:
vault-data:Bind mounts:
volumes:
# Read-only configuration
- ./configs/postgres/postgresql.conf:/etc/postgresql/postgresql.conf:ro
# Read-write data
- ./data:/data
# Vault keys (external)
- ~/.config/vault:/vault-keys:ro
# Multiple mounts
- ./configs/service/config.yml:/etc/service/config.yml:ro
- ./configs/service/scripts:/scripts:ro
- service-data:/var/lib/serviceNetwork definition:
networks:
dev-services:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16
gateway: 172.20.0.1Service network:
services:
postgres:
networks:
dev-services:
ipv4_address: 172.20.0.10From .env file:
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PORT: ${POSTGRES_PORT}Hardcoded values:
environment:
ENVIRONMENT: development
LOG_LEVEL: infoFile-based:
env_file:
- .env
- .env.localAdd new service:
services:
myservice:
image: myservice:latest
container_name: dev-myservice
depends_on:
vault:
condition: service_healthy
environment:
VAULT_ADDR: http://vault:8200
VAULT_TOKEN: ${VAULT_TOKEN}
volumes:
- ./configs/myservice/init.sh:/init/init.sh:ro
networks:
dev-services:
ipv4_address: 172.20.0.30
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
restart: unless-stoppedOverride service:
# docker-compose.override.yml
services:
postgres:
ports:
- "5433:5432"
environment:
POSTGRES_MAX_CONNECTIONS: 500