Skip to content

Commit 8d32cab

Browse files
authored
Merge pull request #2740 from dvdksn/src-attr-secret-env
docs: clarify options for secret types (file, env)
2 parents 1de3325 + 239930c commit 8d32cab

File tree

1 file changed

+67
-14
lines changed

1 file changed

+67
-14
lines changed

docs/reference/buildx_build.md

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ For example, the following Dockerfile contains four stages:
626626
```dockerfile
627627
# syntax=docker/dockerfile:1
628628

629-
FROM oven/bun:1 as base
629+
FROM oven/bun:1 AS base
630630
WORKDIR /app
631631

632632
FROM base AS install
@@ -912,17 +912,39 @@ For more information about how to use build secrets, see
912912

913913
Supported types are:
914914

915-
- [`file`](#file)
916-
- [`env`](#env)
915+
- [`type=file`](#typefile)
916+
- [`type=env`](#typeenv)
917917

918-
Buildx attempts to detect the `type` automatically if unset.
918+
Buildx attempts to detect the `type` automatically if unset. If an environment
919+
variable with the same key as `id` is set, then Buildx uses `type=env` and the
920+
variable value becomes the secret. If no such environment variable is set, and
921+
`type` is not set, then Buildx falls back to `type=file`.
919922

920-
#### `file`
923+
#### `type=file`
921924

922-
Attribute keys:
925+
Source a build secret from a file.
926+
927+
##### `type=file` synopsis
928+
929+
```console
930+
$ docker buildx build --secret [type=file,]id=<ID>[,src=<FILEPATH>] .
931+
```
932+
933+
##### `type=file` attributes
934+
935+
| Key | Description | Default |
936+
| --------------- | ----------------------------------------------------------------------------------------------------- | -------------------------- |
937+
| `id` | ID of the secret. | N/A (this key is required) |
938+
| `src`, `source` | Filepath of the file containing the secret value (absolute or relative to current working directory). | `id` if unset. |
939+
940+
###### `type=file` usage
923941

924-
- `id` - ID of the secret. Defaults to base name of the `src` path.
925-
- `src`, `source` - Secret filename. `id` used if unset.
942+
In the following example, `type=file` is automatically detected because no
943+
environment variable mathing `aws` (the ID) is set.
944+
945+
```console
946+
$ docker buildx build --secret id=aws,src=$HOME/.aws/credentials .
947+
```
926948

927949
```dockerfile
928950
# syntax=docker/dockerfile:1
@@ -932,16 +954,31 @@ RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
932954
aws s3 cp s3://... ...
933955
```
934956

957+
#### `type=env`
958+
959+
Source a build secret from an environment variable.
960+
961+
##### `type=env` synopsis
962+
935963
```console
936-
$ docker buildx build --secret id=aws,src=$HOME/.aws/credentials .
964+
$ docker buildx build --secret [type=env,]id=<ID>[,env=<VARIABLE>] .
937965
```
938966

939-
#### `env`
967+
##### `type=env` attributes
940968

941-
Attribute keys:
969+
| Key | Description | Default |
970+
| ---------------------- | ----------------------------------------------- | -------------------------- |
971+
| `id` | ID of the secret. | N/A (this key is required) |
972+
| `env`, `src`, `source` | Environment variable to source the secret from. | `id` if unset. |
973+
974+
##### `type=env` usage
975+
976+
In the following example, `type=env` is automatically detected because an
977+
environment variable matching `id` is set.
942978

943-
- `id` - ID of the secret. Defaults to `env` name.
944-
- `env` - Secret environment variable. `id` used if unset, otherwise will look for `src`, `source` if `id` unset.
979+
```console
980+
$ SECRET_TOKEN=token docker buildx build --secret id=SECRET_TOKEN .
981+
```
945982

946983
```dockerfile
947984
# syntax=docker/dockerfile:1
@@ -951,10 +988,26 @@ RUN --mount=type=bind,target=. \
951988
yarn run test
952989
```
953990

991+
In the following example, the build argument `SECRET_TOKEN` is set to contain
992+
the value of the environment variable `API_KEY`.
993+
954994
```console
955-
$ SECRET_TOKEN=token docker buildx build --secret id=SECRET_TOKEN .
995+
$ API_KEY=token docker buildx build --secret id=SECRET_TOKEN,env=API_KEY .
956996
```
957997

998+
You can also specify the name of the environment variable with `src` or `source`:
999+
1000+
```console
1001+
$ API_KEY=token docker buildx build --secret type=env,id=SECRET_TOKEN,src=API_KEY .
1002+
```
1003+
1004+
> [!NOTE]
1005+
> Specifying the environment variable name with `src` or `source`, you are
1006+
> required to set `type=env` explicitly, or else Buildx assumes that the secret
1007+
> is `type=file`, and looks for a file with the name of `src` or `source` (in
1008+
> this case, a file named `API_KEY` relative to the location where the `docker
1009+
> buildx build` command was executed.
1010+
9581011
### <a name="shm-size"></a> Shared memory size for build containers (--shm-size)
9591012

9601013
Sets the size of the shared memory allocated for build containers when using

0 commit comments

Comments
 (0)