DevOpsInterview.Cloud
Docker

Docker Image Hardening - Security Checklist

2 min read

A Docker image that works isn't the same as a Docker image that's safe to run in production. This question shows up in almost every DevOps and platform interview because container security mistakes are common, cheap to make, and expensive to clean up after.

The answer interviewers want

Start with a minimal base image. Something like Alpine gives you a much smaller attack surface than a full OS image, since there's less installed software that could carry vulnerabilities.

Always run containers as a non-root user. If an attacker breaks out of your application, you don't want them landing with root privileges inside the container.

Scan your images with a tool like trivy before they ship. Catching known CVEs in your base image or dependencies before deployment is a lot cheaper than catching them after.

Never embed secrets in the image itself. No API keys, no credentials, no tokens baked into layers, environment variables, or config files that get committed to the image. Secrets belong in a secrets manager or injected at runtime, not in the build.

Pin your package versions. Don't let a apt-get install somepackage silently pull in whatever the latest version happens to be on build day. Pin it so your builds are reproducible and you know exactly what's in the image.

Use multi-stage builds. Compile or build your application in one stage, then copy only the final artifacts into a clean, minimal final-stage image. This keeps build-time secrets, source code, and build tooling out of the image that actually ships to production.

Put these together and you've got an image with a small attack surface, no embedded secrets, known and pinned dependencies, and no unnecessary root access.

Why interviewers ask this

This question separates people who have only ever written a working Dockerfile from people who've had to think about what happens when that image is sitting in a registry or running in a cluster that gets scanned by a security team. Interviewers want to know you treat the image itself as part of the attack surface, not just a delivery mechanism for code.

Follow-ups to expect

How would you handle scanning in CI so bad images never reach production? Run the Trivy scan as a pipeline step and fail the build on high or critical findings, rather than scanning after the image is already deployed.

What do you do if a non-root user can't bind to certain ports or write to certain paths? Adjust the application or container configuration, such as using unprivileged ports or explicitly granted write permissions, rather than reverting to root as a shortcut.

Why not just rely on scanning instead of multi-stage builds? Scanning catches known vulnerabilities, but it won't stop secrets or build tooling from sitting in image layers; multi-stage builds remove that risk at the source.

The common wrong answer

Candidates often answer with a single tactic, usually "scan the image with Trivy," and stop there, as if scanning alone makes an image production-ready. That misses that a scanned image can still run as root, still ship with embedded secrets, or still carry an unpinned dependency that changes under you on the next build. Hardening is a checklist, not a single tool.

Prefer it spoken? Watch the 30-second answer on YouTube

More Docker questions