How do you harden a Docker image and container at build and run time?
What they are really testing: Senior signal: knows the default is root, never bakes secrets into layers, and applies runtime hardening. Distinct from generic supply-chain scanning, this is the Dockerfile/runtime craft.
A real interview question
How do you harden a Docker image and container at build and run time?
What most people say
drag me
“I would scan the image for vulnerabilities before deploying it.”
Scanning is one part, but it ignores the biggest Dockerfile mistakes: running as root by default and baking secrets into layers, plus runtime hardening. A senior addresses build and run, not just scanning.
The follow-ups they ask next
Why is putting a secret in an ENV or ARG dangerous even if you delete it later?
Image layers are immutable and additive: the secret remains in the layer history and can be extracted, even if a later instruction removes it. Use BuildKit secret mounts or runtime injection so it never lands in a layer.
What does running as non-root actually protect against?
It limits the blast radius of a container escape or app compromise: an attacker starts unprivileged, so combined with dropped capabilities and no-new-privileges it is much harder to break out to the host.
What the interviewer is listening for
- Knows default is root, sets non-root USER
- Knows secrets persist in layer history; uses BuildKit/runtime injection
- Runtime hardening: read-only FS, cap-drop, no-new-privileges
What sinks the answer
- Only "scan the image"
- Runs as root / bakes secrets into ENV
- Uses --privileged casually
If you genuinely do not know
Say this instead of freezing. Reasoning out loud from what you do know beats silence every single time, and a good interviewer is listening for exactly that.
“Build time: [set a non-root USER (default is root)], [keep secrets out of layers, they persist in history, use BuildKit --mount=type=secret], [minimal base pinned by digest + CI scanning]. Run time: [read-only root FS, --cap-drop, no-new-privileges, avoid --privileged]. Mindset: [least privilege end to end, assume the container can be compromised].”
Keep going with docker
Foundation
What is a container, and how is it different from a virtual machine?
Foundation
What is the difference between a Docker image and a container?
Foundation
What is a Dockerfile, and what do the main instructions do?
Junior
How does the Docker build cache work, and how do you order a Dockerfile to use it well?
Junior
What is the difference between CMD and ENTRYPOINT, and what is the exec versus shell form?
Junior
How does container networking work in Docker, and how do containers talk to each other?
Knowing the answer is not the same as recalling it under pressure
Sign in to send the questions you fumble to spaced recall, so they come back right before you would forget them, and learn the concepts behind them with hands-on labs.
Start free