2.9 KiB

name, description
name description
add-docker-secrets Wire credentials (API keys, tokens, private package-index auth) into a Docker / docker-compose project without baking them into image layers — BuildKit secret mounts for build time, env_file or compose secrets for runtime, plus .gitignore / .dockerignore hygiene checks. Use when the user says "docker secrets", "add a secret", "API key in the container", "token for the build", needs credentials inside a containerized project (e.g. NCBI/Entrez keys, GitHub PAT for private R or Python packages), or asks how to pass secrets into docker build or docker compose.

add-docker-secrets

When to use

  • User says "docker secrets", "add a secret", "/add-docker-secrets".
  • A containerized project needs a credential: API key at runtime, or a token to install private packages at build time.

Process

  1. Classify the secret — build time or runtime?
    • Build time: needed only while the image builds (private PyPI index, GitHub PAT for renv/remotes installs).
    • Runtime: needed by the running code (NCBI/Entrez key, database password, service token).
  2. Build-time → BuildKit secret mount. Never ARG/ENV:
    RUN --mount=type=secret,id=gh_pat \
        GITHUB_PAT=$(cat /run/secrets/gh_pat) R -e "renv::restore()"
    
    # docker-compose.yml
    services:
      dev:
        build:
          secrets: [gh_pat]
    secrets:
      gh_pat:
        file: ./secrets/gh_pat.txt
    
    The secret exists only during that RUN; it never lands in a layer.
  3. Runtime → env_file (simple) or compose secrets: (file-based).
    services:
      dev:
        env_file: .env        # KEY=value lines, never committed
    
    Code reads Sys.getenv("ENTREZ_KEY") / os.environ["ENTREZ_KEY"] — no fallback default containing a real value.
  4. Hygiene check — all three must hold before finishing:
    • .env, secrets/ in .gitignore.
    • .env, .env.*, secrets/ in .dockerignore (so COPY . . can't leak them into a layer).
    • docker history <image> shows no secret in any layer command.
  5. Provide a .env.example with placeholder values so the required keys are documented without the values.

Output / done criteria

  • Secret reaches the build or the process, and docker history plus git status show no trace of its value.
  • .env.example lists every required key.

Guardrails

  • Never pass secrets via ARG or ENV in a Dockerfile — both persist in image metadata and layer history.
  • Never COPY a file containing a secret into any stage.
  • Never commit .env or secrets/; check .gitignore before writing them.
  • Never echo a secret value into logs, justfile recipes, or compose environment: literals.

See also