Merge pull request #15182 from mapuri/build-arg
Support for passing build-time variables in build context Upstream-commit: 1ffff4c8e2b208664b6f63ea6cd3c4c3dace3cc9 Component: engine
This commit is contained in:
@ -86,7 +86,7 @@ This section lists each version from latest to oldest. Each listing includes a
|
||||
* `GET /containers/(id)/stats` will return networking information respectively for each interface.
|
||||
* The `hostConfig` option now accepts the field `DnsOptions`, which specifies a
|
||||
list of DNS options to be used in the container.
|
||||
|
||||
* `POST /build` now optionally takes a serialized map of build-time variables.
|
||||
|
||||
### v1.20 API changes
|
||||
|
||||
|
||||
@ -1367,6 +1367,11 @@ Query Parameters:
|
||||
- **memswap** - Total memory (memory + swap), `-1` to disable swap.
|
||||
- **cpushares** - CPU shares (relative weight).
|
||||
- **cpusetcpus** - CPUs in which to allow execution (e.g., `0-3`, `0,1`).
|
||||
- **buildargs** – JSON map of string pairs for build-time variables. Users pass
|
||||
these values at build-time. Docker uses the `buildargs` as the environment
|
||||
context for command(s) run via the Dockerfile's `RUN` instruction or for
|
||||
variable expansion in other Dockerfile instructions. This is not meant for
|
||||
passing secret values. [Read more about the buildargs instruction](/reference/builder/#arg)
|
||||
|
||||
Request Headers:
|
||||
|
||||
|
||||
@ -966,6 +966,128 @@ For example:
|
||||
The output of the final `pwd` command in this `Dockerfile` would be
|
||||
`/path/$DIRNAME`
|
||||
|
||||
## ARG
|
||||
|
||||
ARG <name>[=<default value>]
|
||||
|
||||
The `ARG` instruction defines a variable that users can pass at build-time to
|
||||
the builder with the `docker build` command using the `--build-arg
|
||||
<varname>=<value>` flag. If a user specifies a build argument that was not
|
||||
defined in the Dockerfile, the build outputs an error.
|
||||
|
||||
```
|
||||
One or more build-args were not consumed, failing build.
|
||||
```
|
||||
|
||||
The Dockerfile author can define a single variable by specifying `ARG` once or many
|
||||
variables by specifying `ARG` more than once. For example, a valid Dockerfile:
|
||||
|
||||
```
|
||||
FROM busybox
|
||||
ARG user1
|
||||
ARG buildno
|
||||
...
|
||||
```
|
||||
|
||||
A Dockerfile author may optionally specify a default value for an `ARG` instruction:
|
||||
|
||||
```
|
||||
FROM busybox
|
||||
ARG user1=someuser
|
||||
ARG buildno=1
|
||||
...
|
||||
```
|
||||
|
||||
If an `ARG` value has a default and if there is no value passed at build-time, the
|
||||
builder uses the default.
|
||||
|
||||
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
|
||||
elsewhere. For example, consider this Dockerfile:
|
||||
|
||||
```
|
||||
1 FROM busybox
|
||||
2 USER ${user:-some_user}
|
||||
3 ARG user
|
||||
4 USER $user
|
||||
...
|
||||
```
|
||||
A user builds this file by calling:
|
||||
|
||||
```
|
||||
$ docker build --build-arg user=what_user Dockerfile
|
||||
```
|
||||
|
||||
The `USER` at line 2 evaluates to `some_user` as the `user` variable is defined on the
|
||||
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
|
||||
`ARG` instruction, any use of a variable results in an empty string.
|
||||
|
||||
> **Note:** It is not recommended to use build-time variables for
|
||||
> passing secrets like github keys, user credentials etc.
|
||||
|
||||
You can use an `ARG` or an `ENV` instruction to specify variables that are
|
||||
available to the `RUN` instruction. Environment variables defined using the
|
||||
`ENV` instruction always override an `ARG` instruction of the same name. Consider
|
||||
this Dockerfile with an `ENV` and `ARG` instruction.
|
||||
|
||||
```
|
||||
1 FROM ubuntu
|
||||
2 ARG CONT_IMG_VER
|
||||
3 ENV CONT_IMG_VER v1.0.0
|
||||
4 RUN echo $CONT_IMG_VER
|
||||
```
|
||||
Then, assume this image is built with this command:
|
||||
|
||||
```
|
||||
$ docker build --build-arg CONT_IMG_VER=v2.0.1 Dockerfile
|
||||
```
|
||||
|
||||
In this case, the `RUN` instruction uses `v1.0.0` instead of the `ARG` setting
|
||||
passed by the user:`v2.0.1` This behavior is similar to a shell
|
||||
script where a locally scoped variable overrides the variables passed as
|
||||
arguments or inherited from environment, from its point of definition.
|
||||
|
||||
Using the example above but a different `ENV` specification you can create more
|
||||
useful interactions between `ARG` and `ENV` instructions:
|
||||
|
||||
```
|
||||
1 FROM ubuntu
|
||||
2 ARG CONT_IMG_VER
|
||||
3 ENV CONT_IMG_VER ${CONT_IMG_VER:-v1.0.0}
|
||||
4 RUN echo $CONT_IMG_VER
|
||||
```
|
||||
|
||||
Unlike an `ARG` instruction, `ENV` values are always persisted in the built
|
||||
image. Consider a docker build without the --build-arg flag:
|
||||
|
||||
```
|
||||
$ docker build Dockerfile
|
||||
```
|
||||
|
||||
Using this Dockerfile example, `CONT_IMG_VER` is still persisted in the image but
|
||||
its value would be `v1.0.0` as it is the default set in line 3 by the `ENV` instruction.
|
||||
|
||||
The variable expansion technique in this example allows you to pass arguments
|
||||
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
|
||||
Dockerfile instructions.](#environment-replacement)
|
||||
|
||||
Docker has a set of predefined `ARG` variables that you can use without a
|
||||
corresponding `ARG` instruction in the Dockerfile.
|
||||
|
||||
* `HTTP_PROXY`
|
||||
* `http_proxy`
|
||||
* `HTTPS_PROXY`
|
||||
* `https_proxy`
|
||||
* `FTP_PROXY`
|
||||
* `ftp_proxy`
|
||||
* `NO_PROXY`
|
||||
* `no_proxy`
|
||||
|
||||
To use these, simply pass them on the command line using the `--build-arg
|
||||
<varname>=<value>` flag.
|
||||
|
||||
## ONBUILD
|
||||
|
||||
ONBUILD [INSTRUCTION]
|
||||
|
||||
@ -17,6 +17,7 @@ weight=1
|
||||
|
||||
-f, --file="" Name of the Dockerfile (Default is 'PATH/Dockerfile')
|
||||
--force-rm=false Always remove intermediate containers
|
||||
--build-arg=[] Set build-time variables
|
||||
--no-cache=false Do not use cache when building the image
|
||||
--pull=false Always attempt to pull a newer version of the image
|
||||
-q, --quiet=false Suppress the verbose output generated by the containers
|
||||
@ -251,3 +252,22 @@ flag](/reference/run/#specifying-custom-cgroups).
|
||||
Using the `--ulimit` option with `docker build` will cause each build step's
|
||||
container to be started using those [`--ulimit`
|
||||
flag values](/reference/run/#setting-ulimits-in-a-container).
|
||||
|
||||
You can use `ENV` instructions in a Dockerfile to define variable
|
||||
values. These values persist in the built image. However, often
|
||||
persistence is not what you want. Users want to specify variables differently
|
||||
depending on which host they build an image on.
|
||||
|
||||
A good example is `http_proxy` or source versions for pulling intermediate
|
||||
files. The `ARG` instruction lets Dockerfile authors define values that users
|
||||
can set at build-time using the `---build-arg` flag:
|
||||
|
||||
$ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 .
|
||||
|
||||
This flag allows you to pass the build-time variables that are
|
||||
accessed like regular environment variables in the `RUN` instruction of the
|
||||
Dockerfile. Also, these values don't persist in the intermediate or final images
|
||||
like `ENV` values do.
|
||||
|
||||
For detailed information on using `ARG` and `ENV` instructions, see the
|
||||
[Dockerfile reference](/reference/builder).
|
||||
|
||||
Reference in New Issue
Block a user