Docker Production Best Practices

Running Docker on a laptop and running it for real customers are two different jobs. Production systems must stay fast, safe, and recoverable. Experienced teams follow a short list of habits that prevent most outages, and each habit below builds on the earlier topics.

The Production Mindset

A home kitchen can survive a burned dish. A restaurant kitchen needs checklists, labeled ingredients, clean tools, and backup plans. Production Docker follows the restaurant model.

Development              Production
-------------------      ------------------------------
quick and flexible       repeatable and predictable
one user                 many users
restart when broken      recover automatically

Build Practices

Use Small, Pinned Base Images

FROM python:3.12.3-slim

Pinned versions give identical builds. Slim images reduce size and exposure.

Use Multi Stage Builds

Compile code in a build stage and copy only the finished program into the final image. The final image then carries no compilers or source files.

Order Instructions for Caching

Copy dependency files first, install packages, and copy the source code last. Code changes then reuse the cached dependency layer.

Keep a Dockerignore File

Exclude Git history, local settings, and secrets from the build context.

Runtime Practices

One Process per Container

Give each container a single job. A web server, a database, and a worker belong in separate containers, which scale and fail independently.

Run as a Non-Root User

USER 10001

Set Resource Limits

docker run -d --memory=512m --cpus=1 my-app

Add Health Checks

Health checks let orchestrators replace broken containers without human help.

Choose a Restart Policy

docker run -d --restart unless-stopped my-app

Data Practices

  • Store persistent data in named volumes and never inside the container layer.
  • Back up volumes on a schedule and test the restore process.
  • Treat containers as disposable. Any container should be replaceable at any moment.
Backup a volume:
docker run --rm -v mydata:/data -v $(pwd):/backup alpine \
  tar czf /backup/mydata.tar.gz -C /data .

Configuration and Secrets

Keep one image for every environment and change behavior through environment variables and secret files. An image built once and promoted from testing to production removes the risk of "it worked on my machine."

Same image  --+--> development  (DEBUG=true)
              +--> staging      (DEBUG=false)
              +--> production   (DEBUG=false, real secrets)

Security Practices

  • Scan every image before release.
  • Drop unneeded capabilities and use a read-only file system.
  • Keep the Docker socket private.
  • Update the host, the engine, and base images regularly.

Logging and Monitoring

  • Print logs to standard output.
  • Set log rotation limits.
  • Collect metrics for CPU, memory, restarts, and response times.
  • Create alerts for unhealthy containers and full disks.

Release Practices

  1. Tag images with a commit ID or version number.
  2. Deploy with rolling updates.
  3. Keep the previous version ready for a fast rollback.
  4. Watch dashboards during and after each release.

Housekeeping

docker system df              show disk use
docker image prune -a         remove unused images
docker container prune        remove stopped containers
docker volume prune           remove unused volumes (check first)

Old images and stopped containers pile up quietly. Schedule cleanup, and review volume removal carefully because volumes hold real data.

Production Readiness Checklist

[ ] Image pinned, small, and scanned
[ ] Non-root user
[ ] Resource limits set
[ ] Health check defined
[ ] Logs rotate and ship to a central place
[ ] Secrets stored outside the image
[ ] Data volumes backed up
[ ] Rolling update and rollback tested

Review this list before every launch. A few minutes of review prevent long nights of repair.

Real-World Scenario

Two teams launch similar applications in the same month. Team A ships a large image with the latest tag, runs as root, skips health checks, and stores uploads inside the container. Team B ships a slim pinned image, runs as a regular user, defines a health check, and stores uploads in a backed-up volume. In the third week, both teams face a disk failure on the host. Team A loses every upload and spends two days rebuilding. Team B restores the volume from last night's backup and returns to normal in under an hour.

Start Small and Improve

Nobody adopts every practice on day one. Rank the habits by risk and effort. Pinned versions, a non-root user, and log limits take minutes and prevent common failures. Scanning, backups, and rolling updates take more planning and deliver large benefits. Add one improvement per week, and your setup grows stronger without overwhelming the team.