Skip to content

Commit fba1b10

Browse files
committed
docs: fix incorrect information about arg scoping
ARGs are inherited by downstream build stages, if declared in a base stage. The documentation ambiguously stated that ARGs are out of scope in other stages, where it really meant other stages that are not child/downstream stages. Signed-off-by: David Karlsson <[email protected]>
1 parent 8193e88 commit fba1b10

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

frontend/dockerfile/docs/reference.md

+16-19
Original file line numberDiff line numberDiff line change
@@ -2349,9 +2349,8 @@ at build-time, the builder uses the default.
23492349

23502350
### Scope
23512351

2352-
An `ARG` variable definition comes into effect from the line on which it is
2353-
defined in the Dockerfile not from the argument's use on the command-line or
2354-
elsewhere. For example, consider this Dockerfile:
2352+
An `ARG` variable comes into effect from the line on which it is declared in
2353+
the Dockerfile. For example, consider this Dockerfile:
23552354

23562355
```dockerfile
23572356
FROM busybox
@@ -2367,24 +2366,22 @@ A user builds this file by calling:
23672366
$ docker build --build-arg username=what_user .
23682367
```
23692368

2370-
The `USER` at line 2 evaluates to `some_user` as the `username` variable is defined on the
2371-
subsequent line 3. The `USER` at line 4 evaluates to `what_user`, as the `username` argument is
2372-
defined and the `what_user` value was passed on the command line. Prior to its definition by an
2373-
`ARG` instruction, any use of a variable results in an empty string.
2369+
- The `USER` instruction on line 2 evaluates to the `some_user` fallback,
2370+
because the `username` variable is not yet declared.
2371+
- The `username` variable is declared on line 3, and available for reference in
2372+
Dockerfile instruction from that point onwards.
2373+
- The `USER` instruction on line 4 evaluates to `what_user`, since at that
2374+
point the `username` argument has a value of `what_user` which was passed on
2375+
the command line. Prior to its definition by an `ARG` instruction, any use of
2376+
a variable results in an empty string.
23742377

2375-
An `ARG` instruction goes out of scope at the end of the build
2376-
stage where it was defined. To use an argument in multiple stages, each stage must
2377-
include the `ARG` instruction.
2378+
An `ARG` variable declared within a build stage is automatically inherited by
2379+
other stages based on that stage. Unrelated build stages do not have access to
2380+
the variable. To use an argument in multiple distinct stages, each stage must
2381+
include the `ARG` instruction, or they must both be based on a shared base
2382+
stage in the same Dockerfile where the variable is declared.
23782383

2379-
```dockerfile
2380-
FROM busybox
2381-
ARG SETTINGS
2382-
RUN ./run/setup $SETTINGS
2383-
2384-
FROM busybox
2385-
ARG SETTINGS
2386-
RUN ./run/other $SETTINGS
2387-
```
2384+
For more information, refer to [variable scoping](https://docs.docker.com/build/building/variables/#scoping).
23882385

23892386
### Using ARG variables
23902387

0 commit comments

Comments
 (0)