Merge pull request #333 from dnephin/docs-arg-in-from-take-2

Improve docs about ARG in FROM
This commit is contained in:
Sebastiaan van Stijn 2017-07-14 18:04:53 -07:00 committed by GitHub
commit fe24806ee5
1 changed files with 38 additions and 15 deletions

View File

@ -530,15 +530,16 @@ FROM extras:${CODE_VERSION}
CMD /code/run-extras CMD /code/run-extras
``` ```
To use the default value of an `ARG` declared before the first `FROM` use an An `ARG` declared before a `FROM` is outside of a build stage, so it
`ARG` instruction without a value: can't be used in any instruction after a `FROM`. To use the default value of
an `ARG` declared before the first `FROM` use an `ARG` instruction without
a value inside of a build stage:
```Dockerfile ```Dockerfile
ARG SETTINGS=default ARG VERSION=latest
FROM busybox:$VERSION
FROM busybox ARG VERSION
ARG SETTINGS RUN echo $VERSION > image_version
``` ```
## RUN ## RUN
@ -1364,8 +1365,8 @@ defined in the Dockerfile, the build outputs a warning.
[Warning] One or more build-args [foo] were not consumed. [Warning] One or more build-args [foo] were not consumed.
``` ```
The Dockerfile author can define a single variable by specifying `ARG` once or many A Dockerfile may include one or more `ARG` instructions. For example,
variables by specifying `ARG` more than once. For example, a valid Dockerfile: the following is a valid Dockerfile:
``` ```
FROM busybox FROM busybox
@ -1374,7 +1375,13 @@ ARG buildno
... ...
``` ```
A Dockerfile author may optionally specify a default value for an `ARG` instruction: > **Warning:** It is not recommended to use build-time variables for
> passing secrets like github keys, user credentials etc. Build-time variable
> values are visible to any user of the image with the `docker history` command.
### Default values
An `ARG` instruction can optionally include a default value:
``` ```
FROM busybox FROM busybox
@ -1383,8 +1390,10 @@ ARG buildno=1
... ...
``` ```
If an `ARG` value has a default and if there is no value passed at build-time, the If an `ARG` instruction has a default value and if there is no value passed
builder uses the default. at build-time, the builder uses the default.
### Scope
An `ARG` variable definition comes into effect from the line on which it is An `ARG` variable definition comes into effect from the line on which it is
defined in the `Dockerfile` not from the argument's use on the command-line or defined in the `Dockerfile` not from the argument's use on the command-line or
@ -1408,9 +1417,21 @@ subsequent line 3. The `USER` at line 4 evaluates to `what_user` as `user` is
defined and the `what_user` value was passed on the command line. Prior to its definition by an defined and the `what_user` value was passed on the command line. Prior to its definition by an
`ARG` instruction, any use of a variable results in an empty string. `ARG` instruction, any use of a variable results in an empty string.
> **Warning:** It is not recommended to use build-time variables for An `ARG` instruction goes out of scope at the end of the build
> passing secrets like github keys, user credentials etc. Build-time variable stage where it was defined. To use an arg in multiple stages, each stage must
> values are visible to any user of the image with the `docker history` command. include the `ARG` instruction.
```
FROM busybox
ARG SETTINGS
RUN ./run/setup $SETTINGS
FROM busybox
ARG SETTINGS
RUN ./run/other $SETTINGS
```
### Using ARG variables
You can use an `ARG` or an `ENV` instruction to specify variables that are You can use an `ARG` or an `ENV` instruction to specify variables that are
available to the `RUN` instruction. Environment variables defined using the available to the `RUN` instruction. Environment variables defined using the
@ -1459,6 +1480,8 @@ from the command line and persist them in the final image by leveraging the
`ENV` instruction. Variable expansion is only supported for [a limited set of `ENV` instruction. Variable expansion is only supported for [a limited set of
Dockerfile instructions.](#environment-replacement) Dockerfile instructions.](#environment-replacement)
### Predefined ARGs
Docker has a set of predefined `ARG` variables that you can use without a Docker has a set of predefined `ARG` variables that you can use without a
corresponding `ARG` instruction in the Dockerfile. corresponding `ARG` instruction in the Dockerfile.