Managing Docker in Production
1. Implementing Rolling Updates
| Option | Values | Notes |
|---|---|---|
| --update-delay | 10s |
Delay between updates of each container |
| --update-parallelism | 1 |
Number of containers to update in parallel |
| --update-failure-action | rollback |
Action on update failure (rollback, pause, continue) |
| --update-monitor | 5s |
Monitoring interval for update progress |
Example: Update Service with Rolling Strategy
docker service update \
--update-parallelism 1 \
--update-delay 10s \
--update-monitor 5s \
--update-failure-action rollback \
my-web-app
Note: Rolling updates ensure zero-downtime deployment by updating containers one at a time.
Warning: Ensure your application can handle concurrent versions during rolling updates.
Rolling Update Best Practices
- Set
--update-parallelismto control update concurrency - Use
--update-delayto prevent overwhelming the system - Configure
--update-failure-actionto handle rollback scenarios - Monitor with
--update-monitorfor real-time progress
2. Configuring Zero-Downtime Deployments
Example: Using Docker Compose for Rolling Updates
version: '3.8'
services:
web:
image: myapp:latest
deploy:
replicas: 2
update_config:
parallelism: 1
delay: 10s
failure_action: rollback
rollback_config:
parallelism: 1
delay: 5s
| Configuration | Description | Default Value | Notes |
|---|---|---|---|
| deploy.update_config.parallelism | Number of containers to update at once | 1 |
Set to 0 for all at once |
| deploy.update_config.delay | Delay between updates | 0s |
Use 10s for stability |
| deploy.update_config.failure_action | Action on update failure | continue |
Use rollback for zero-downtime |
| deploy.rollback_config.parallelism | Number of containers to rollback at once | 1 |
Same as update config |
Note: Ensure your application handles graceful shutdowns to avoid data loss during updates.
Warning: Rolling updates may cause temporary service unavailability if not configured correctly.
Zero-Downtime Deployment Best Practices
- Use
parallelism: 1to update one container at a time - Add
delaybetween updates to allow health checks - Set
failure_action: rollbackfor automatic recovery - Implement graceful shutdown logic in your app
3. Setting Up Health Monitoring
Example: Docker Healthcheck Configuration
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
CMD ["npm", "start"]
| Option | Values | Description |
|---|---|---|
| --interval | 30s |
How often to run the health check |
| --timeout | 10s |
Maximum time to wait for the check to complete |
| --start-period | 5s |
Grace period before starting health checks |
| --retries | 3 |
Number of consecutive failures to mark container as unhealthy |
Note: The
HEALTHCHECK instruction is supported in Dockerfile and can be overridden at runtime using docker run --healthcheck.Example: Healthcheck with Custom Script
# healthcheck.sh
#!/bin/bash
set -e
curl --fail http://localhost:3000/health && \
pg_isready -U postgres -d mydb || exit 1
Example: Monitoring with Prometheus
scrape_configs:
- job_name: 'docker'
static_configs:
- targets: ['localhost:9323']
4. Implementing Log Aggregation
Example: Basic Docker Log Configuration
version: '3.8'
services:
app:
image: myapp:latest
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
| Driver | Description | Usage |
|---|---|---|
| json-file | Default driver storing logs in JSON format | Best for local development and basic setups |
| syslog | Sends logs to syslog daemon | Use with centralized logging systems like Graylog or ELK |
| fluentd | Logs to Fluentd for aggregation and forwarding | Recommended for production environments with structured logging |
| journald | Uses systemd-journald for log storage | Best on systems using systemd, such as Ubuntu 20.04+ |
Example: Using Fluentd for Log Aggregation
version: '3.8'
services:
app:
image: myapp:latest
logging:
driver: fluentd
options:
fluentd-address: "localhost:24224"
tag: "app.logs"
Note: Ensure that your logging driver supports structured log formats for better parsing and analysis in tools like ELK Stack or Grafana Loki.
Warning: Without proper log rotation, logs can quickly consume disk space. Always configure
max-size and max-file options.
Best Practices for Log Aggregation
- Use structured logging formats (JSON) to enable parsing and filtering
- Implement log rotation to prevent disk space exhaustion
- Centralize logs using tools like Fluentd, Logstash, or Vector
- Tag logs consistently for easy filtering and search
- Monitor log volume to detect anomalies or performance issues
5. Using Centralized Logging
Example: Configure Docker to send logs to a central logging service
docker run --log-driver=syslog \
--log-opt syslog-address=udp://log-server.example.com:514 \
--log-opt tag=docker-app \
my-app-image
| Driver | Description | Notes |
|---|---|---|
| json-file | Default driver storing logs in JSON format | Logs stored in /var/lib/docker/containers/<container-id>/<container-id>-json.log |
| syslog | Sends logs to syslog-compatible services | Supports UDP/TCP and various syslog formats |
| fluentd | Logs to Fluentd for centralized collection | Requires fluentd daemon running on host or in container |
| journald | Uses systemd-journald for logging | Best for systems using systemd |
Note: For production, consider using a dedicated logging solution like ELK stack or Fluentd to aggregate and analyze logs from multiple containers.
6. Configuring Backup Strategies
Example: Docker Volume Backup Script
#!/bin/bash
# Backup Docker volumes to tar.gz archive
VOLUME_NAME="myapp_data"
BACKUP_DIR="/backups"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/backup_$VOLUME_NAME-$DATE.tar.gz"
docker run --rm \
-v $VOLUME_NAME:/data \
-v $BACKUP_DIR:/backup \
alpine tar czf /backup/$BACKUP_FILE -C /data .
echo "Backup created: $BACKUP_FILE"
| Strategy | Description | Tools | Notes |
|---|---|---|---|
| Volume Backup | Backup data volumes to external storage | Docker CLI, tar, cloud storage | Use docker run --rm -v volume:/data alpine tar czf backup.tar.gz -C /data . |
| Image Snapshot | Save container images to registry or local storage | Docker CLI, Docker Registry | Use docker save or push to registry |
| Stateful Backup | Backup configuration and state of running containers | Docker Compose, Kubernetes | Use docker inspect to capture container state |
Note: Regular backups should be automated and tested for restore capability.
Warning: Ensure backup storage is secure and has sufficient capacity.
Backup Best Practices
- Automate backups with cron jobs or orchestration tools
- Store backups in multiple locations (local + cloud)
- Test restore procedures regularly
- Encrypt sensitive backup data
- Retain backups for a defined retention period
7. Implementing Disaster Recovery
Example: Backup and Restore Strategy
# Backup Docker volumes
docker run --rm -v /var/lib/docker/volumes/myapp_data/_data:/data -v backup_volume:/backup alpine tar czf /backup/backup.tar.gz -C /data .
# Restore Docker volumes
docker run --rm -v /var/lib/docker/volumes/myapp_data/_data:/data -v backup_volume:/backup alpine tar xzf /backup/backup.tar.gz -C /data
| Recovery Method | Description | Use Case | Notes |
|---|---|---|---|
| Volume Backup | Archiving Docker volumes to external storage | Data persistence in case of container failure | Use tools like tar or rsync |
| Image Snapshot | Save Docker image state to registry | Quick recovery of application stack | Use docker save or docker push |
| Stateful Recovery | Rebuilding containers from backup state | Disaster recovery for complex services | Requires orchestration tools like Kubernetes |
Note: Always test recovery procedures regularly to ensure data integrity.
Warning: Ensure backup storage is encrypted and access is restricted.
Disaster Recovery Best Practices
- Automate backups with cron jobs or orchestration tools
- Store backups in multiple geographic locations
- Validate backup integrity periodically
- Document recovery steps clearly for team members
8. Managing Secrets Securely
| Secret Type | Description | Usage | Security Notes |
|---|---|---|---|
| Docker Secrets | Managed secrets within Docker Swarm mode | docker secret create mysecret < secret.txt |
Encrypted at rest, accessible only to authorized services |
| Environment Variables | Pass secrets via docker run -e |
docker run -e DB_PASSWORD=secret123 myapp |
Not encrypted in logs or process list |
| External Secret Stores | Use tools like HashiCorp Vault or AWS Secrets Manager | docker run --secret vault-secret myapp |
Centralized, auditable, and encrypted |
Example: Using Docker Secrets in Swarm
docker secret create db_password <<EOF
my_secure_password
EOF
docker service create --secret db_password --env DB_PASSWORD_FILE=/run/secrets/db_password myapp
Note: Docker secrets are only available in Swarm mode. For standalone containers, use external secret stores or environment variables with caution.
Warning: Never commit secrets to source control or expose them in logs. Use
docker secret inspect to verify access.
Best Practices for Secret Management
- Use external secret stores (e.g., Vault, AWS Secrets Manager) for production
- Avoid hardcoding secrets in Dockerfiles or image builds
- Rotate secrets regularly and audit access logs
- Use
docker secretin Swarm for simple internal secrets
9. Setting Up Access Control
| Control Type | Description | Implementation |
|---|---|---|
| User Roles | Define roles with specific permissions | docker group create <group-name> |
| Resource Limits | Restrict resource usage per user or group | docker run --memory=512m --cpus=0.5 |
| Network Isolation | Control network access between containers | docker network create --driver bridge isolated-net |
Example: Creating a Secure User Group
docker group create prod-users
docker group add-member prod-users admin@example.com
Note: Ensure that users have appropriate permissions to manage containers and images within their designated namespaces.
Warning: Misconfigured access control can lead to unauthorized container execution or data exposure.
10. Monitoring Container Metrics
| Metric | Description | Collection Method | Notes |
|---|---|---|---|
| CPU Usage | Percentage of CPU time used by the container | docker stats |
Can be collected in real-time or periodically |
| Memory Usage | Amount of RAM consumed by the container | docker stats |
Includes memory limit and usage limits |
| Network I/O | Bytes sent and received over the network | docker stats |
Can be monitored per container or globally |
| Disk I/O | Read and write operations to disk | docker stats |
Requires --io flag for detailed I/O stats |
Example: Using Prometheus to Monitor Docker Metrics
scrape_configs:
- job_name: 'docker'
static_configs:
- targets: ['localhost:9323']
Note: Docker stats provides real-time metrics but does not persist data. For long-term monitoring, integrate with tools like Prometheus or Grafana.
11. Implementing Auto-Scaling
| Option | Values | Description |
|---|---|---|
| docker service update --replicas | 5 |
Scale service to 5 replicas |
| docker service update --rollback | --rollback |
Rollback to previous version |
Example: Auto-scaling with External Tools
# Using Docker Swarm with Prometheus and Grafana<br>docker service create --name prometheus \\<br> --publish 9090:9090 \\<br> prom/prometheus
Note: Auto-scaling in Docker Swarm requires integration with monitoring tools like Prometheus or external orchestration systems.
12. Managing Production Deployments
Example: Docker Compose for Production
version: '3.8'
services:
web:
image: myapp:latest
deploy:
replicas: 3
environment:
- NODE_ENV=production
ports:
- "80:3000"
db:
image: postgres:15
deploy:
replicas: 1
environment:
- POSTGRES_DB=myapp
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
| Deployment Strategy | Description | Notes |
|---|---|---|
| rolling | rolling |
Gradually replace old containers with new ones |
| recreate | recreate |
Stop and remove old containers before starting new ones |
| ha | ha |
High availability with load balancing |
Note: Use
deploy configuration in Docker Compose for production-level deployment strategies.Example: Health Checks in Production
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
CMD ["node", "server.js"]
| Healthcheck Option | Default Value | Description |
|---|---|---|
| --interval | 30s | Time between health checks |
| --timeout | 30s | Maximum time to wait for a check |
| --start-period | 0s | Grace period before checks start |
| --retries | 3 | Number of consecutive failures to mark container as unhealthy |