Skip to main content
InterviewHubPRO
Section Overview
Q19

Question 19 of 50

Part 2: Core Architecture & Implementation (Mid-Level)

38%
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 ARG or ENV are permanently visible in docker history <image> and stored in plaintext image metadata layers!

The 3 Production-Grade Secret Patterns:

  1. BuildKit Secret Mounts (Build time):
    dockerfile
    # Secrets are mounted in RAM temporarily during RUN and NOT committed to layers
    RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm publish
    
    Build command: docker build --secret id=npmrc,src=$HOME/.npmrc .
  2. Environment File at Runtime:
    bash
    docker run --env-file /secure/path/.env.production my-app
    
  3. Secret Store Integration (Vault, AWS Secrets Manager, Kubernetes Secrets): Inject secrets directly into /var/run/secrets/ in-memory volume at runtime.