---
title: "Pass build secrets as mounts instead of · Agent Wranglers"
url: https://agentwranglers.se/playbook/operations/containers/build-secrets
updated: 2026-10-20
licence: "All rights reserved"
---

# Pass build secrets as mounts instead of layers

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Containers](https://agentwranglers.se/playbook/operations/containers)
- Rule: Give the build a token or key only through a secret mount. Never through `ARG`, `ENV`, or a copied file.
- How: Build arguments and environment variables stay readable in the image and its history. With BuildKit, write `RUN --mount=type=secret,id=npm_token,env=NPM_TOKEN npm ci` and build with `--secret id=npm_token`. The secret exists only while that step runs. Runtime secrets come from the [secrets store](https://agentwranglers.se/playbook/engineering/running-in-production/secrets-store).
- Check: `docker history --no-trunc shop` and a secret scanner run over the saved image find no token.

Example:

```dockerfile
# Bad: the token stays in the image history
ARG NPM_TOKEN
RUN NPM_TOKEN=$NPM_TOKEN npm ci

# Good: mounted for one step only
RUN --mount=type=secret,id=npm_token,env=NPM_TOKEN npm ci
```

```sh
docker build --secret id=npm_token,env=NPM_TOKEN -t shop .
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers operations playbook: https://agentwranglers.se/playbook/operations/containers/build-secrets`.

- Previous: [Send only the files the build needs](https://agentwranglers.se/playbook/operations/containers/dockerignore)
- Next: [Let the main process receive stop signals](https://agentwranglers.se/playbook/operations/containers/container-signals)
- [Containers](https://agentwranglers.se/playbook/operations/containers)
