Docker & Containers (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How do you manage secrets securely in Docker without baking them into image layers?
senior LevelMost AskedCode ExampleDocker & ContainersSystem Design
In Simple Words:
Never use ENV or ARG for sensitive tokens or passwords.
- Values passed to
ARGorENVare permanently visible indocker history <image>and stored in plaintext image metadata layers!
The 3 Production-Grade Secret Patterns:
- BuildKit Secret Mounts (Build time):
Build command:dockerfile
# Secrets are mounted in RAM temporarily during RUN and NOT committed to layers RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm publishdocker build --secret id=npmrc,src=$HOME/.npmrc . - Environment File at Runtime:
bash
docker run --env-file /secure/path/.env.production my-app - Secret Store Integration (Vault, AWS Secrets Manager, Kubernetes Secrets):
Inject secrets directly into
/var/run/secrets/in-memory volume at runtime.