Skip to content

Commit 9462420

Browse files
committed
build: run mount secrets as env
Signed-off-by: David Karlsson <[email protected]>
1 parent df66fbd commit 9462420

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

content/manuals/build/building/secrets.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ secret mounts or SSH mounts, which expose secrets to your builds securely.
1616

1717
## Secret mounts
1818

19-
Secret mounts expose secrets to the build containers as files. You [mount the
20-
secrets to the `RUN`
21-
instructions](/reference/dockerfile.md#run---mounttypesecret) that
19+
Secret mounts expose secrets to the build containers, as files or environment
20+
variables. You can use secret mounts to pass sensitive information to your
21+
builds, such as API tokens, passwords, or SSH keys. You [mount the secrets to
22+
the `RUN` instructions](/reference/dockerfile.md#run---mounttypesecret) that
2223
need to access them, similar to how you would define a bind mount or cache
2324
mount.
2425

25-
```dockerfile
26-
RUN --mount=type=secret,id=mytoken \
27-
TOKEN=$(cat /run/secrets/mytoken) ...
28-
```
26+
### Passing secrets
2927

3028
To pass a secret to a build, use the [`docker build --secret`
3129
flag](/reference/cli/docker/buildx/build.md#secret), or the
@@ -82,21 +80,40 @@ $ docker build --secret id=API_TOKEN .
8280

8381
### Target
8482

85-
By default, secrets are mounted to `/run/secrets/<id>`. You can customize the
86-
mount point in the build container using the `target` option in the Dockerfile.
83+
By default, secrets are mounted as files located at `/run/secrets/<id>`. You
84+
can customize how the secrets get mounted in the build container using the
85+
`target` and `env` options for the `RUN --mount` flag in the Dockerfile.
8786

88-
The following example mounts the secret to a `/root/.aws/credentials` file in
89-
the build container.
87+
The following example takes secret id `aws` and mounts it to `/run/secrets/aws`
88+
in the build container.
9089

91-
```console
92-
$ docker build --secret id=aws,src=/root/.aws/credentials .
90+
```dockerfile
91+
RUN --mount=type=secret,id=aws \
92+
AWS_SHARED_CREDENTIALS_FILE=/run/secrets/aws \
93+
aws s3 cp ...
9394
```
9495

96+
To mount a secret as a file with a different name, use the `target` option in
97+
the `--mount` flag.
98+
9599
```dockerfile
96100
RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
97101
aws s3 cp ...
98102
```
99103

104+
To mount a secret as an environment variable instead of a file, use the
105+
`env` option in the `--mount` flag.
106+
107+
```dockerfile
108+
RUN --mount=type=secret,id=aws-key-id,env=AWS_ACCESS_KEY_ID \
109+
--mount=type=secret,id=aws-secret-key,env=AWS_SECRET_ACCESS_KEY \
110+
--mount=type=secret,id=aws-session-token,env=AWS_SESSION_TOKEN \
111+
aws s3 cp ...
112+
```
113+
114+
It's possible to use the `target` and `env` options together to mount a secret
115+
as both a file and an environment variable.
116+
100117
## SSH mounts
101118

102119
If the credential you want to use in your build is an SSH agent socket or key,

content/manuals/build/cache/invalidation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ Build arguments do result in cache invalidation.
8282
```dockerfile
8383
FROM alpine
8484
ARG CACHEBUST
85-
RUN --mount=type=secret,id=foo \
86-
TOKEN=$(cat /run/secrets/foo) ...
85+
RUN --mount=type=secret,id=TOKEN,env=TOKEN \
86+
some-command ...
8787
```
8888

8989
```console
90-
$ TOKEN=verysecret docker build --secret id=foo,env=TOKEN --build-arg CACHEBUST=1 .
90+
$ TOKEN="tkn_pat123456" docker build --secret id=TOKEN --build-arg CACHEBUST=1 .
9191
```
9292

9393
Properties of secrets such as IDs and mount paths do participate in the cache

content/manuals/build/ci/github-actions/secrets.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ First, create a `Dockerfile` that uses the secret:
2626
```dockerfile
2727
# syntax=docker/dockerfile:1
2828
FROM alpine
29-
RUN --mount=type=secret,id=github_token \
30-
cat /run/secrets/github_token
29+
RUN --mount=type=secret,id=github_token,env=GITHUB_TOKEN ...
3130
```
3231

3332
In this example, the secret name is `github_token`. The following workflow

0 commit comments

Comments
 (0)