Add Dockerfile reference docs for using ARG in FROM
| ... | ... |
@@ -135,9 +135,11 @@ The instruction is not case-sensitive. However, convention is for them to |
| 135 | 135 |
be UPPERCASE to distinguish them from arguments more easily. |
| 136 | 136 |
|
| 137 | 137 |
|
| 138 |
-Docker runs instructions in a `Dockerfile` in order. **The first |
|
| 139 |
-instruction must be \`FROM\`** in order to specify the [*Base |
|
| 140 |
-Image*](glossary.md#base-image) from which you are building. |
|
| 138 |
+Docker runs instructions in a `Dockerfile` in order. A `Dockerfile` **must |
|
| 139 |
+start with a \`FROM\` instruction**. The `FROM` instruction specifies the [*Base |
|
| 140 |
+Image*](glossary.md#base-image) from which you are building. `FROM` may only be |
|
| 141 |
+proceeded by one or more `ARG` instructions, which declare arguments that are used |
|
| 142 |
+in `FROM` lines in the `Dockerfile`. |
|
| 141 | 143 |
|
| 142 | 144 |
Docker treats lines that *begin* with `#` as a comment, unless the line is |
| 143 | 145 |
a valid [parser directive](#parser-directives). A `#` marker anywhere |
| ... | ... |
@@ -356,11 +358,12 @@ the `Dockerfile`: |
| 356 | 356 |
* `COPY` |
| 357 | 357 |
* `ENV` |
| 358 | 358 |
* `EXPOSE` |
| 359 |
+* `FROM` |
|
| 359 | 360 |
* `LABEL` |
| 361 |
+* `STOPSIGNAL` |
|
| 360 | 362 |
* `USER` |
| 361 |
-* `WORKDIR` |
|
| 362 | 363 |
* `VOLUME` |
| 363 |
-* `STOPSIGNAL` |
|
| 364 |
+* `WORKDIR` |
|
| 364 | 365 |
|
| 365 | 366 |
as well as: |
| 366 | 367 |
|
| ... | ... |
@@ -371,14 +374,14 @@ as well as: |
| 371 | 371 |
> variable, even when combined with any of the instructions listed above. |
| 372 | 372 |
|
| 373 | 373 |
Environment variable substitution will use the same value for each variable |
| 374 |
-throughout the entire command. In other words, in this example: |
|
| 374 |
+throughout the entire instruction. In other words, in this example: |
|
| 375 | 375 |
|
| 376 | 376 |
ENV abc=hello |
| 377 | 377 |
ENV abc=bye def=$abc |
| 378 | 378 |
ENV ghi=$abc |
| 379 | 379 |
|
| 380 | 380 |
will result in `def` having a value of `hello`, not `bye`. However, |
| 381 |
-`ghi` will have a value of `bye` because it is not part of the same command |
|
| 381 |
+`ghi` will have a value of `bye` because it is not part of the same instruction |
|
| 382 | 382 |
that set `abc` to `bye`. |
| 383 | 383 |
|
| 384 | 384 |
## .dockerignore file |
| ... | ... |
@@ -469,7 +472,7 @@ All of the README files are included. The middle line has no effect because |
| 469 | 469 |
|
| 470 | 470 |
You can even use the `.dockerignore` file to exclude the `Dockerfile` |
| 471 | 471 |
and `.dockerignore` files. These files are still sent to the daemon |
| 472 |
-because it needs them to do its job. But the `ADD` and `COPY` commands |
|
| 472 |
+because it needs them to do its job. But the `ADD` and `COPY` instructions |
|
| 473 | 473 |
do not copy them to the image. |
| 474 | 474 |
|
| 475 | 475 |
Finally, you may want to specify which files to include in the |
| ... | ... |
@@ -492,24 +495,40 @@ Or |
| 492 | 492 |
|
| 493 | 493 |
The `FROM` instruction initializes a new build stage and sets the |
| 494 | 494 |
[*Base Image*](glossary.md#base-image) for subsequent instructions. As such, a |
| 495 |
-valid `Dockerfile` must have `FROM` as its first instruction. The image can be |
|
| 495 |
+valid `Dockerfile` must start with a `FROM` instruction. The image can be |
|
| 496 | 496 |
any valid image – it is especially easy to start by **pulling an image** from |
| 497 | 497 |
the [*Public Repositories*](https://docs.docker.com/engine/tutorials/dockerrepos/). |
| 498 | 498 |
|
| 499 |
-- `FROM` must be the first non-comment instruction in the `Dockerfile`. |
|
| 499 |
+- `ARG` is the only instruction that may proceed `FROM` in the `Dockerfile`. |
|
| 500 |
+ See [Understand how ARG and FROM interact](#understand-how-arg-and-from-interact). |
|
| 500 | 501 |
|
| 501 |
-- `FROM` can appear multiple times within a single `Dockerfile` in order to |
|
| 502 |
-create multiple images or use one build stage as a dependency for another. |
|
| 503 |
-Simply make a note of the last image ID output by the commit before each new |
|
| 504 |
-`FROM` command. Each `FROM` command resets all the previous commands. |
|
| 502 |
+- `FROM` can appear multiple times within a single `Dockerfile` to |
|
| 503 |
+ create multiple images or use one build stage as a dependency for another. |
|
| 504 |
+ Simply make a note of the last image ID output by the commit before each new |
|
| 505 |
+ `FROM` instruction. Each `FROM` instruction clears any state created by previous |
|
| 506 |
+ instructions. |
|
| 505 | 507 |
|
| 506 |
-- Optionally a name can be given to a new build stage. That name can be then |
|
| 507 |
-used in subsequent `FROM` and `COPY --from=<name|index>` commands to refer back |
|
| 508 |
-to the image built in this stage. |
|
| 508 |
+- Optionally a name can be given to a new build stage by adding `AS name` to the |
|
| 509 |
+ `FROM` instruction. The name can be used in subsequent `FROM` and |
|
| 510 |
+ `COPY --from=<name|index>` instructions to refer to the image built in this stage. |
|
| 509 | 511 |
|
| 510 | 512 |
- The `tag` or `digest` values are optional. If you omit either of them, the |
| 511 |
-builder assumes a `latest` tag by default. The builder returns an error if it |
|
| 512 |
-cannot match the `tag` value. |
|
| 513 |
+ builder assumes a `latest` tag by default. The builder returns an error if it |
|
| 514 |
+ cannot find the `tag` value. |
|
| 515 |
+ |
|
| 516 |
+### Understand how ARG and FROM interact |
|
| 517 |
+ |
|
| 518 |
+`FROM` instructions support variables that are declared by any `ARG` |
|
| 519 |
+instructions that occur before the first `FROM`. |
|
| 520 |
+ |
|
| 521 |
+```Dockerfile |
|
| 522 |
+ARG CODE_VERSION=latest |
|
| 523 |
+FROM base:${CODE_VERSION}
|
|
| 524 |
+CMD /code/run-app |
|
| 525 |
+ |
|
| 526 |
+FROM extras:${CODE_VERSION}
|
|
| 527 |
+CMD /code/run-extras |
|
| 528 |
+``` |
|
| 513 | 529 |
|
| 514 | 530 |
## RUN |
| 515 | 531 |
|
| ... | ... |
@@ -947,7 +966,7 @@ Optionally `COPY` accepts a flag `--from=<name|index>` that can be used to set |
| 947 | 947 |
the source location to a previous build stage (created with `FROM .. AS <name>`) |
| 948 | 948 |
that will be used instead of a build context sent by the user. The flag also |
| 949 | 949 |
accepts a numeric index assigned for all previous build stages started with |
| 950 |
-`FROM` command. In case a build stage with a specified name can't be found an |
|
| 950 |
+`FROM` instruction. In case a build stage with a specified name can't be found an |
|
| 951 | 951 |
image with the same name is attempted to be used instead. |
| 952 | 952 |
|
| 953 | 953 |
`COPY` obeys the following rules: |
| ... | ... |
@@ -1353,7 +1372,7 @@ elsewhere. For example, consider this Dockerfile: |
| 1353 | 1353 |
A user builds this file by calling: |
| 1354 | 1354 |
|
| 1355 | 1355 |
``` |
| 1356 |
-$ docker build --build-arg user=what_user Dockerfile |
|
| 1356 |
+$ docker build --build-arg user=what_user . |
|
| 1357 | 1357 |
``` |
| 1358 | 1358 |
|
| 1359 | 1359 |
The `USER` at line 2 evaluates to `some_user` as the `user` variable is defined on the |
| ... | ... |
@@ -1379,7 +1398,7 @@ this Dockerfile with an `ENV` and `ARG` instruction. |
| 1379 | 1379 |
Then, assume this image is built with this command: |
| 1380 | 1380 |
|
| 1381 | 1381 |
``` |
| 1382 |
-$ docker build --build-arg CONT_IMG_VER=v2.0.1 Dockerfile |
|
| 1382 |
+$ docker build --build-arg CONT_IMG_VER=v2.0.1 . |
|
| 1383 | 1383 |
``` |
| 1384 | 1384 |
|
| 1385 | 1385 |
In this case, the `RUN` instruction uses `v1.0.0` instead of the `ARG` setting |
| ... | ... |
@@ -1401,7 +1420,7 @@ Unlike an `ARG` instruction, `ENV` values are always persisted in the built |
| 1401 | 1401 |
image. Consider a docker build without the `--build-arg` flag: |
| 1402 | 1402 |
|
| 1403 | 1403 |
``` |
| 1404 |
-$ docker build Dockerfile |
|
| 1404 |
+$ docker build . |
|
| 1405 | 1405 |
``` |
| 1406 | 1406 |
|
| 1407 | 1407 |
Using this Dockerfile example, `CONT_IMG_VER` is still persisted in the image but |