Module 10_ Docker Compose for Multi-Container Applications
Module 10_ Docker Compose for Multi-Container Applications
Multi-Container Applications
Docker & Containerization Workshop
Key Benefits
- "80:80" db-data:
Compose File Versions
Compose Docker Choosing a Version
Version Engine
● Use latest supported by
3.8 19.03.0+
your Docker Engine
3.7 18.06.0+ ● Consider feature
3.0 1.13.0+ requirements
● Address compatibility
2.4 17.12.0+ with deployment
2.0 1.10.0+ environment
1.0 1.9.1+
networks:
- frontend # Network
Service Configuration attachment
max-file: "3"
Environment Variables # Variable substitution
environment:
Multiple Ways to Define Environment Variables - SERVICE_NAME=$
services: {SERVICE_NAME:-webapp}
webapp:
# Individual variables
environment:
- DEBUG=true
-
DATABASE_URL=postgres://postgres:password@db:5432/my
db
# From file
env_file:
- ./common.env
- ./app.env
Networking Configuration
# Network definitions
networks:
Network Types and Configuration
frontend:
services:
driver: bridge
webapp:
driver_opts:
networks:
com.docker.network.bridge.name:
frontend: frontend_bridge
ipv4_address: 172.16.238.10 ipam:
backend: driver: default
db: config:
networks: - subnet: 172.16.238.0/24
- backend backend:
# Volume definitions
Volume Management volumes:
app-data:
Volume Types and Configuration driver: local
webapp: o: addr=10.0.0.10,rw
- app-data:/app/data #
Named volume db-data:
- ./local/path:/app/config # Bind external: true # Use pre-existing
mount volume
- /tmp/cache #
Anonymous volume
db:
Secret Management
# Secret definitions
services: api_key:
secrets: cert_key:
uid: '0'
gid: '0'
mode: 0400
# Check logs
Compose Commands
docker compose logs -f webapp
Lifecycle Management
# Execute command in container
# Start services
docker compose exec webapp sh
docker compose up -d
# Scale services
# Stop services
docker compose up -d --scale
docker compose down webapp=3
# View running services
docker compose ps
Compose Profiles
Using Profiles for Service Groups
Starting with Profiles
services:
docker compose --profile dev up -d
webapp:
image: my-webapp docker compose --profile monitoring up -d
profiles: ["prod", "dev"]
db:
image: postgres
profiles: ["prod", "dev"]
adminer:
image: adminer
profiles: ["dev"]
monitoring:
image: prometheus
profiles: ["monitoring"]
Extend & Override Configurations
Base Configuration Development Override Production Override
# docker-compose.yml # docker- # docker-
(base) compose.override.yml compose.prod.yml
services: services: services:
webapp: webapp: webapp:
image: build: ./webapp restart: always
webapp:latest volumes: deploy:
ports: - ./webapp:/app replicas: 3
- "80:80" environment:
- DEBUG=true
Environment-Specific Configurations
.env File