Docker in Production
Deployment patterns, logging strategies, and knowing when to move beyond Docker Compose
Skills taught: docker.production-deployment, docker.logging, docker.deployment-patterns, docker.orchestration-overview
The deployment spectrum
Not every application needs Kubernetes. Understanding the deployment spectrum helps you choose the right tool for where you are now — and know when to upgrade:
- Bare server + docker compose — 1 server, simple stack, <100k users. Perfect for side projects, MVPs, internal tools. One SSH + docker compose pull + docker compose up -d for deploys.
- Docker Swarm — multi-server, built into Docker, simple replicas and rolling updates. Great for teams that need HA without Kubernetes complexity.
- Managed container platforms — Fly.io, Railway, Render, Google Cloud Run. They handle orchestration; you give them a Dockerfile. Best for teams that want to focus on product.
- Kubernetes — full orchestration, self-healing, auto-scaling, service mesh. Right when you have 10+ services, multiple teams, or complex operational requirements.
Zero-downtime deployment on a single server
#!/usr/bin/env bash
set -euo pipefail
IMAGE="ghcr.io/myorg/api"
TAG=${1:-latest} # pass tag as argument, default to latest
echo "Deploying $IMAGE:$TAG..."
# 1. Pull the new image
docker pull $IMAGE:$TAG
# 2. Start a new container on a different port
docker run -d \
--name api-new \
--network app-network \
-p 3001:3000 \
--env-file /etc/myapp/.env \
--restart unless-stopped \
$IMAGE:$TAG
# 3. Health check the new container
for i in $(seq 1 30); do
if curl -sf http://localhost:3001/health; then
echo "New container healthy!"
break
fi
sleep 2
done
# 4. Update nginx to point to new container
docker exec nginx nginx -s reload
# 5. Stop and remove old container
docker stop api-old && docker rm api-old
# 6. Rename new to old for next deploy
docker rename api-new api-old
echo "Deploy complete: $IMAGE:$TAG"
Production logging strategy
services:
api:
image: myapi
# Docker's default json-file driver with rotation
logging:
driver: json-file
options:
max-size: "10m" # rotate at 10 MB
max-file: "5" # keep 5 files = max 50 MB per container
# For production: forward logs to a central service
# driver: loki (Grafana Loki)
# driver: fluentd
# driver: awslogs (CloudWatch)
nginx:
logging:
driver: loki
options:
loki-url: "http://loki:3100/loki/api/v1/push"
loki-labels: "job=nginx,env=production"
# BEST PRACTICE:
# Apps should log to stdout/stderr ONLY — no log files inside containers
# Docker captures stdout/stderr and routes them to the logging driver
# This keeps the container stateless and lets you centralise logs
Production checklist
- Images built with multi-stage builds — minimal size, no build tools in production
- Images tagged with git SHA — reproducible, traceable, rollback-able
- Non-root user in Dockerfile — reduced breach impact
- No secrets in image layers — passed via env_file or secrets at runtime
- Healthcheck defined on every service — enables proper depends_on and load balancer routing
- restart: unless-stopped on all long-running services — survives crashes and server reboots
- Named volumes for all persistent data — never in container writable layer
- Log rotation configured — prevents disk fill from runaway logging
- Resource limits set — one container can't starve the server
- Regular base image updates — patch known CVEs in the OS and runtime
Frequently Asked Questions
When should I move from Docker Compose to Kubernetes?
When you need: (1) multiple replicas with automatic failover across servers, (2) rolling deployments with zero downtime at scale, (3) horizontal autoscaling based on CPU/memory/custom metrics, (4) 10+ microservices that need independent scaling. For most teams, a managed platform like Fly.io or Cloud Run is the right step before bare Kubernetes — you get orchestration without operating the control plane.