21. Production Best Practices
Before deploying any containerized application to a live production environment, it is critical to audit your infrastructure. What works on a developer’s laptop will often fail—or be easily hacked—in a production environment if best practices are ignored.
Use this comprehensive checklist to harden your Docker deployments.
Images
Section titled “Images”- Small Images: Are you using the smallest possible footprint? (Aim for Alpine, Slim, or Distroless). This speeds up deployments and limits the attack surface.
- Multi-Stage Builds: Does your Dockerfile separate the build environment from the runtime environment? Your production image should never contain compilers, source code, or
devDependencies. - Versioned Images: Are you pulling exact Semantic Versions? (e.g.,
node:18.17.0-alpine). You must never use the:latesttag in production, as an unexpected upstream update will eventually break your application. - Vulnerability Scanning: Is your CI/CD pipeline configured to scan the image for CVEs (using Trivy or Snyk) before pushing it to the registry?
- Trusted Base Images: Are you only using Official Docker Hub images or images built by trusted vendors? Never base your infrastructure on an unverified, community-uploaded image.
Containers
Section titled “Containers”- Non-Root User: Does your Dockerfile include a
USERdirective? The container must not run asroot(UID 0) to prevent severe container breakout attacks. - Minimal Privileges: Have you dropped unnecessary Linux capabilities using
--cap-drop=ALL? Are you absolutely certain you are not using the highly dangerous--privilegedflag? - Resource Limits: Are you strictly limiting memory and CPU in your
compose.yaml? If not, a single memory leak in one container will eventually crash the entire host machine. - Healthchecks: Does every service have a
healthcheckdefined to verify the application is actually functioning, rather than just relying on the PID 1 status? - Restart Policy: Are your background services configured with
restart: unless-stoppedso they automatically recover from transient crashes or host server reboots?
Networking
Section titled “Networking”- Only Required Ports Exposed: Have you double-checked that you are only publishing (
-p) ports that explicitly require public access (like 80 and 443)? - Internal Services Hidden: Databases, Redis caches, and message queues must never be published to the host. They should only be accessible internally via the private Docker bridge network.
- Separate Networks: If running multiple distinct applications on the same server, are they isolated on different custom bridge networks? (e.g., App A cannot ping App B’s database).
Secrets & Configuration
Section titled “Secrets & Configuration”- No Passwords in Dockerfiles: Are you absolutely certain there are no
ENV DATABASE_PASSWORD=...instructions in your Dockerfile? (Baking secrets into the immutable image layer is a critical security flaw). - No Secrets in Git: Is your
.envfile explicitly listed in your.gitignorefile? - Proper Secret Management: Are you passing secrets dynamically at runtime using secure
.envfiles, Docker Secrets, or an external Secret Manager (like HashiCorp Vault)?
Operations
Section titled “Operations”- Logging: Have you configured log rotation (e.g.,
--log-opt max-size=10m) to prevent JSON logs from consuming 100% of the host machine’s hard drive? Are logs being aggregated to a centralized dashboard (like Loki or ELK)? - Monitoring: Do you have visibility into the host’s CPU, Memory, and Disk usage (via Prometheus/Grafana or Datadog) to alert you before resources are exhausted?
- Backups: Are your database volumes being compressed and shipped off-site regularly? Have you performed a “fire drill” to verify the backup isn’t corrupted?
- Rollback Strategy: If a deployment fails catastrophically, do you know the exact Git tag or Image hash required to instantly revert the
compose.yamlto the previous stable version?