Skip to main content
CloudBit Logo
DevOps

Docker in production: best practices for secure, lightweight images

Multi-stage builds, unprivileged users, vulnerability scanning and immutable tags: what separates a lab container from a production-ready one.

· 6 min read · CloudBit team

Multi-stage builds for small images

Separating the build stage from the final image leaves out compilers, development dependencies and temporary files. A smaller image downloads faster, starts sooner and has a smaller attack surface.

As a base, prefer minimal, maintained images (slim, distroless or Alpine when the application tolerates it) and pin the exact version instead of using latest.

Never as root

By default, a container runs as root. Define an unprivileged user in the Dockerfile and, in the orchestrator, a read-only file system with no extra capabilities. If someone exploits the application, the damage stays contained.

Secrets outside the image

Passwords and keys don't belong in the Dockerfile or in build variables: they get baked into the image layers. On AWS they are injected at runtime from Secrets Manager or Parameter Store, with permissions scoped to the service that needs them.

Scanning and immutable tags

Scan every image in the pipeline (Trivy, Grype or Amazon ECR scanning) and block deployment on critical vulnerabilities. In ECR, enable immutable tags and use the commit hash as the tag: you know exactly what code runs in each environment and can roll back with confidence.

Observable containers

Write logs to standard output in a structured format, expose a health endpoint and honor the shutdown signal to finish in-flight connections. The orchestrator does the rest: it collects logs, restarts unhealthy containers and replaces instances without downtime.

Want to apply this at your company?

Book a free 30-minute technical consultation.

Talk to a specialist