Docker for developers · mid
Configuration and secrets: one image, many environments
Build one image and deploy it everywhere, with configuration from the environment at run time. A secret copied into any layer stays there, so use build secret mounts and run-time secret stores.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Build once, deploy many: the exact image digest that passed staging goes to production. Per-environment builds can pull different dependency versions, so production runs something nobody tested.
- Configuration comes at run time from environment variables or mounted files (ConfigMaps, Parameter Store, Secrets Manager). The app validates configuration at startup and fails fast with a clear message.
- ARG exists only during the build. ENV is stored in the image and present at run time. Both are readable from the image, so neither may hold a secret.
- Frontend public variables (such as NEXT_PUBLIC_) are baked into the browser bundle at build time, so they need different handling. Use a run-time config endpoint or build per environment on purpose.
- Layers are immutable. RUN rm .env adds a layer that hides the file, but the earlier COPY . . layer still contains it, and anyone who can pull the image can extract it. A .dockerignore for .env files prevents the copy.
- Build-time secrets: BuildKit secret mounts (RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci), which never land in a layer or the build history.
- Run-time secrets come from the platform (Kubernetes Secrets, ideally synced from a secrets manager), not from files in the image.
- If a secret reached a public image: rotate first (rotation is the fix), then take the image down, and check provider logs for use of the old credential. Deleting the tag is not enough because the image may be pulled or mirrored.
The red pen
Common traps: mistake → fix
Mistake: I deleted the file in the next RUN, so it is gone
Fix: It is still in an earlier layer and can be extracted.
Mistake: ENV is safe because only the container sees it
Fix: ENV values are stored in the image metadata and readable by anyone with the image.
Mistake: A separate image per environment is safer
Fix: It is less safe, because the tested image is not the one in production.
Easy to hard
Questions interviewers ask, easy to hard
- Why build one image for every environment?
- What is the difference between ARG and ENV, and why is neither a place for secrets?
- How do you use a private npm token during a build without leaving it in the image?
- A .env file was found in a public image. What do you do in the first hour?
- How does a Next.js app get different API URLs in staging and production?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.