Docker-DCO-1.1-Signed-off-by: James Turnbull <james@lovedthanlost.net> (github: jamtur01)
| ... | ... |
@@ -4,15 +4,17 @@ page_keywords: builder, docker, Dockerfile, automation, image creation |
| 4 | 4 |
|
| 5 | 5 |
# Dockerfile Reference |
| 6 | 6 |
|
| 7 |
-**Docker can act as a builder** and read instructions from a text *Dockerfile* |
|
| 8 |
-to automate the steps you would otherwise take manually to create an image. |
|
| 9 |
-Executing `docker build` will run your steps and commit them along the way, |
|
| 10 |
-giving you a final image. |
|
| 7 |
+**Docker can build images automatically** by reading the instructions |
|
| 8 |
+from a `Dockerfile`. A `Dockerfile` is a text document that contains all |
|
| 9 |
+the commands you would normally execute manually in order to build a |
|
| 10 |
+Docker image. By calling `docker build` from your terminal, you can have |
|
| 11 |
+Docker build your image step by step, executing the instructions |
|
| 12 |
+successively. |
|
| 11 | 13 |
|
| 12 | 14 |
## Usage |
| 13 | 15 |
|
| 14 | 16 |
To [*build*](../commandline/cli/#cli-build) an image from a source repository, |
| 15 |
-create a description file called Dockerfile at the root of your repository. |
|
| 17 |
+create a description file called `Dockerfile` at the root of your repository. |
|
| 16 | 18 |
This file will describe the steps to assemble the image. |
| 17 | 19 |
|
| 18 | 20 |
Then call `docker build` with the path of your source repository as the argument |
| ... | ... |
@@ -55,13 +57,12 @@ accelerating `docker build` significantly (indicated by `Using cache`): |
| 55 | 55 |
---> 1a5ffc17324d |
| 56 | 56 |
Successfully built 1a5ffc17324d |
| 57 | 57 |
|
| 58 |
-When you're done with your build, you're ready to look into |
|
| 59 |
-[*Pushing a repository to its registry*]( |
|
| 60 |
-/userguide/dockerrepos/#image-push). |
|
| 58 |
+When you're done with your build, you're ready to look into [*Pushing a |
|
| 59 |
+repository to its registry*]( /userguide/dockerrepos/#image-push). |
|
| 61 | 60 |
|
| 62 | 61 |
## Format |
| 63 | 62 |
|
| 64 |
-Here is the format of the Dockerfile: |
|
| 63 |
+Here is the format of the `Dockerfile`: |
|
| 65 | 64 |
|
| 66 | 65 |
# Comment |
| 67 | 66 |
INSTRUCTION arguments |
| ... | ... |
@@ -69,8 +70,8 @@ Here is the format of the Dockerfile: |
| 69 | 69 |
The Instruction is not case-sensitive, however convention is for them to |
| 70 | 70 |
be UPPERCASE in order to distinguish them from arguments more easily. |
| 71 | 71 |
|
| 72 |
-Docker evaluates the instructions in a Dockerfile in order. **The first |
|
| 73 |
-instruction must be \`FROM\`** in order to specify the [*Base |
|
| 72 |
+Docker runs the instructions in a `Dockerfile` in order. **The |
|
| 73 |
+first instruction must be \`FROM\`** in order to specify the [*Base |
|
| 74 | 74 |
Image*](/terms/image/#base-image-def) from which you are building. |
| 75 | 75 |
|
| 76 | 76 |
Docker will treat lines that *begin* with `#` as a |
| ... | ... |
@@ -80,10 +81,10 @@ be treated as an argument. This allows statements like: |
| 80 | 80 |
# Comment |
| 81 | 81 |
RUN echo 'we are running some # of cool things' |
| 82 | 82 |
|
| 83 |
-Here is the set of instructions you can use in a Dockerfile |
|
| 84 |
-for building images. |
|
| 83 |
+Here is the set of instructions you can use in a `Dockerfile` for building |
|
| 84 |
+images. |
|
| 85 | 85 |
|
| 86 |
-## .dockerignore |
|
| 86 |
+## The `.dockerignore` file |
|
| 87 | 87 |
|
| 88 | 88 |
If a file named `.dockerignore` exists in the source repository, then it |
| 89 | 89 |
is interpreted as a newline-separated list of exclusion patterns. |
| ... | ... |
@@ -124,15 +125,15 @@ Or |
| 124 | 124 |
FROM <image>:<tag> |
| 125 | 125 |
|
| 126 | 126 |
The `FROM` instruction sets the [*Base Image*](/terms/image/#base-image-def) |
| 127 |
-for subsequent instructions. As such, a valid Dockerfile must have `FROM` as |
|
| 127 |
+for subsequent instructions. As such, a valid `Dockerfile` must have `FROM` as |
|
| 128 | 128 |
its first instruction. The image can be any valid image – it is especially easy |
| 129 | 129 |
to start by **pulling an image** from the [*Public Repositories*]( |
| 130 | 130 |
/userguide/dockerrepos/#using-public-repositories). |
| 131 | 131 |
|
| 132 |
-`FROM` must be the first non-comment instruction in the Dockerfile. |
|
| 132 |
+`FROM` must be the first non-comment instruction in the `Dockerfile`. |
|
| 133 | 133 |
|
| 134 |
-`FROM` can appear multiple times within a single Dockerfile in order to create |
|
| 135 |
-multiple images. Simply make a note of the last image id output by the commit |
|
| 134 |
+`FROM` can appear multiple times within a single `Dockerfile` in order to create |
|
| 135 |
+multiple images. Simply make a note of the last image ID output by the commit |
|
| 136 | 136 |
before each new `FROM` command. |
| 137 | 137 |
|
| 138 | 138 |
If no `tag` is given to the `FROM` instruction, `latest` is assumed. If the |
| ... | ... |
@@ -154,7 +155,7 @@ RUN has 2 forms: |
| 154 | 154 |
|
| 155 | 155 |
The `RUN` instruction will execute any commands in a new layer on top of the |
| 156 | 156 |
current image and commit the results. The resulting committed image will be |
| 157 |
-used for the next step in the Dockerfile. |
|
| 157 |
+used for the next step in the `Dockerfile`. |
|
| 158 | 158 |
|
| 159 | 159 |
Layering `RUN` instructions and generating commits conforms to the core |
| 160 | 160 |
concepts of Docker where commits are cheap and containers can be created from |
| ... | ... |
@@ -163,11 +164,11 @@ any point in an image's history, much like source control. |
| 163 | 163 |
The *exec* form makes it possible to avoid shell string munging, and to `RUN` |
| 164 | 164 |
commands using a base image that does not contain `/bin/sh`. |
| 165 | 165 |
|
| 166 |
-The cache for `RUN` instructions isn't invalidated automatically during the |
|
| 167 |
-next build. The cache for an instruction like `RUN apt-get dist-upgrade -y` |
|
| 168 |
-will be reused during the next build. |
|
| 169 |
-The cache for `RUN` instructions can be invalidated by using the `--no-cache` |
|
| 170 |
-flag, for example `docker build --no-cache`. |
|
| 166 |
+The cache for `RUN` instructions isn't invalidated automatically during |
|
| 167 |
+the next build. The cache for an instruction like `RUN apt-get |
|
| 168 |
+dist-upgrade -y` will be reused during the next build. The cache for |
|
| 169 |
+`RUN` instructions can be invalidated by using the `--no-cache` flag, |
|
| 170 |
+for example `docker build --no-cache`. |
|
| 171 | 171 |
|
| 172 | 172 |
The cache for `RUN` instructions can be invalidated by `ADD` instructions. See |
| 173 | 173 |
[below](#add) for details. |
| ... | ... |
@@ -178,28 +179,27 @@ The cache for `RUN` instructions can be invalidated by `ADD` instructions. See |
| 178 | 178 |
permissions problems that can occur when using the AUFS file system. You |
| 179 | 179 |
might notice it during an attempt to `rm` a file, for example. The issue |
| 180 | 180 |
describes a workaround. |
| 181 |
-- [Issue 2424](https://github.com/dotcloud/docker/issues/2424) Locale will |
|
| 182 |
- not be set automatically. |
|
| 183 | 181 |
|
| 184 | 182 |
## CMD |
| 185 | 183 |
|
| 186 |
-CMD has three forms: |
|
| 184 |
+The `CMD` instruction has three forms: |
|
| 187 | 185 |
|
| 188 | 186 |
- `CMD ["executable","param1","param2"]` (like an *exec*, this is the preferred form) |
| 189 | 187 |
- `CMD ["param1","param2"]` (as *default parameters to ENTRYPOINT*) |
| 190 | 188 |
- `CMD command param1 param2` (as a *shell*) |
| 191 | 189 |
|
| 192 |
-There can only be one CMD in a Dockerfile. If you list more than one CMD |
|
| 193 |
-then only the last CMD will take effect. |
|
| 190 |
+There can only be one `CMD` instruction in a `Dockerfile`. If you list more than one `CMD` |
|
| 191 |
+then only the last `CMD` will take effect. |
|
| 194 | 192 |
|
| 195 |
-**The main purpose of a CMD is to provide defaults for an executing |
|
| 193 |
+**The main purpose of a `CMD` is to provide defaults for an executing |
|
| 196 | 194 |
container.** These defaults can include an executable, or they can omit |
| 197 |
-the executable, in which case you must specify an ENTRYPOINT as well. |
|
| 195 |
+the executable, in which case you must specify an `ENTRYPOINT` |
|
| 196 |
+instruction as well. |
|
| 198 | 197 |
|
| 199 | 198 |
When used in the shell or exec formats, the `CMD` instruction sets the command |
| 200 | 199 |
to be executed when running the image. |
| 201 | 200 |
|
| 202 |
-If you use the *shell* form of the CMD, then the `<command>` will execute in |
|
| 201 |
+If you use the *shell* form of the `CMD`, then the `<command>` will execute in |
|
| 203 | 202 |
`/bin/sh -c`: |
| 204 | 203 |
|
| 205 | 204 |
FROM ubuntu |
| ... | ... |
@@ -207,7 +207,7 @@ If you use the *shell* form of the CMD, then the `<command>` will execute in |
| 207 | 207 |
|
| 208 | 208 |
If you want to **run your** `<command>` **without a shell** then you must |
| 209 | 209 |
express the command as a JSON array and give the full path to the executable. |
| 210 |
-**This array form is the preferred format of CMD.** Any additional parameters |
|
| 210 |
+**This array form is the preferred format of `CMD`.** Any additional parameters |
|
| 211 | 211 |
must be individually expressed as strings in the array: |
| 212 | 212 |
|
| 213 | 213 |
FROM ubuntu |
| ... | ... |
@@ -218,7 +218,7 @@ you should consider using `ENTRYPOINT` in combination with `CMD`. See |
| 218 | 218 |
[*ENTRYPOINT*](#entrypoint). |
| 219 | 219 |
|
| 220 | 220 |
If the user specifies arguments to `docker run` then they will override the |
| 221 |
-default specified in CMD. |
|
| 221 |
+default specified in `CMD`. |
|
| 222 | 222 |
|
| 223 | 223 |
> **Note**: |
| 224 | 224 |
> don't confuse `RUN` with `CMD`. `RUN` actually runs a command and commits |
| ... | ... |
@@ -264,24 +264,23 @@ being built (also called the *context* of the build) or a remote file URL. |
| 264 | 264 |
`<dest>` is the absolute path to which the source will be copied inside the |
| 265 | 265 |
destination container. |
| 266 | 266 |
|
| 267 |
-All new files and directories are created with a uid and gid of 0. |
|
| 267 |
+All new files and directories are created with a UID and GID of 0. |
|
| 268 | 268 |
|
| 269 |
-In the case where `<src>` is a remote file URL, the destination will have permissions 600. |
|
| 269 |
+In the case where `<src>` is a remote file URL, the destination will |
|
| 270 |
+have permissions of 600. |
|
| 270 | 271 |
|
| 271 | 272 |
> **Note**: |
| 272 |
-> If you build by passing a Dockerfile through STDIN (`docker build - < somefile`), |
|
| 273 |
-> there is no build context, so the Dockerfile can only contain a URL |
|
| 274 |
-> based ADD statement. |
|
| 275 |
- |
|
| 276 |
-> You can also pass a compressed archive through STDIN: |
|
| 277 |
-> (`docker build - < archive.tar.gz`), the `Dockerfile` at the root of |
|
| 278 |
-> the archive and the rest of the archive will get used at the context |
|
| 279 |
-> of the build. |
|
| 280 |
-> |
|
| 273 |
+> If you build by passing a `Dockerfile` through STDIN (`docker |
|
| 274 |
+> build - < somefile`), there is no build context, so the `Dockerfile` |
|
| 275 |
+> can only contain a URL based `ADD` instruction. You can also pass a |
|
| 276 |
+> compressed archive through STDIN: (`docker build - < archive.tar.gz`), |
|
| 277 |
+> the `Dockerfile` at the root of the archive and the rest of the |
|
| 278 |
+> archive will get used at the context of the build. |
|
| 279 |
+ |
|
| 281 | 280 |
> **Note**: |
| 282 |
-> If your URL files are protected using authentication, you will need to |
|
| 283 |
-> use `RUN wget` , `RUN curl` |
|
| 284 |
-> or use another tool from within the container as ADD does not support |
|
| 281 |
+> If your URL files are protected using authentication, you |
|
| 282 |
+> will need to use `RUN wget`, `RUN curl` or use another tool from |
|
| 283 |
+> within the container as the `ADD` instruction does not support |
|
| 285 | 284 |
> authentication. |
| 286 | 285 |
|
| 287 | 286 |
> **Note**: |
| ... | ... |
@@ -314,9 +313,9 @@ The copy obeys the following rules: |
| 314 | 314 |
from *remote* URLs are **not** decompressed. When a directory is copied or |
| 315 | 315 |
unpacked, it has the same behavior as `tar -x`: the result is the union of: |
| 316 | 316 |
|
| 317 |
- 1. whatever existed at the destination path and |
|
| 318 |
- 2. the contents of the source tree, with conflicts resolved in favor of |
|
| 319 |
- "2." on a file-by-file basis. |
|
| 317 |
+ 1. Whatever existed at the destination path and |
|
| 318 |
+ 2. The contents of the source tree, with conflicts resolved in favor |
|
| 319 |
+ of "2." on a file-by-file basis. |
|
| 320 | 320 |
|
| 321 | 321 |
- If `<src>` is any other kind of file, it is copied individually along with |
| 322 | 322 |
its metadata. In this case, if `<dest>` ends with a trailing slash `/`, it |
| ... | ... |
@@ -342,7 +341,7 @@ being built (also called the *context* of the build). |
| 342 | 342 |
`<dest>` is the absolute path to which the source will be copied inside the |
| 343 | 343 |
destination container. |
| 344 | 344 |
|
| 345 |
-All new files and directories are created with a uid and gid of 0. |
|
| 345 |
+All new files and directories are created with a UID and GID of 0. |
|
| 346 | 346 |
|
| 347 | 347 |
> **Note**: |
| 348 | 348 |
> If you build using STDIN (`docker build - < somefile`), there is no |
| ... | ... |
@@ -431,34 +430,34 @@ instructions via the Docker client, refer to [*Share Directories via Volumes*]( |
| 431 | 431 |
|
| 432 | 432 |
USER daemon |
| 433 | 433 |
|
| 434 |
-The `USER` instruction sets the username or UID to use when running the image |
|
| 434 |
+The `USER` instruction sets the user name or UID to use when running the image |
|
| 435 | 435 |
and for any following `RUN` directives. |
| 436 | 436 |
|
| 437 | 437 |
## WORKDIR |
| 438 | 438 |
|
| 439 | 439 |
WORKDIR /path/to/workdir |
| 440 | 440 |
|
| 441 |
-The `WORKDIR` instruction sets the working directory for the `RUN`, `CMD` and |
|
| 442 |
-`ENTRYPOINT` Dockerfile commands that follow it. |
|
| 441 |
+The `WORKDIR` instruction sets the working directory for any `RUN`, `CMD` and |
|
| 442 |
+`ENTRYPOINT` instructions that follow it in the `Dockerfile`. |
|
| 443 | 443 |
|
| 444 |
-It can be used multiple times in the one Dockerfile. If a relative path |
|
| 444 |
+It can be used multiple times in the one `Dockerfile`. If a relative path |
|
| 445 | 445 |
is provided, it will be relative to the path of the previous `WORKDIR` |
| 446 | 446 |
instruction. For example: |
| 447 | 447 |
|
| 448 | 448 |
WORKDIR /a WORKDIR b WORKDIR c RUN pwd |
| 449 | 449 |
|
| 450 |
-The output of the final `pwd` command in this |
|
| 451 |
-Dockerfile would be `/a/b/c`. |
|
| 450 |
+The output of the final `pwd` command in this Dockerfile would be |
|
| 451 |
+`/a/b/c`. |
|
| 452 | 452 |
|
| 453 | 453 |
## ONBUILD |
| 454 | 454 |
|
| 455 | 455 |
ONBUILD [INSTRUCTION] |
| 456 | 456 |
|
| 457 |
-The `ONBUILD` instruction adds to the image a |
|
| 458 |
-"trigger" instruction to be executed at a later time, when the image is |
|
| 459 |
-used as the base for another build. The trigger will be executed in the |
|
| 460 |
-context of the downstream build, as if it had been inserted immediately |
|
| 461 |
-after the *FROM* instruction in the downstream Dockerfile. |
|
| 457 |
+The `ONBUILD` instruction adds to the image a *trigger* instruction to |
|
| 458 |
+be executed at a later time, when the image is used as the base for |
|
| 459 |
+another build. The trigger will be executed in the context of the |
|
| 460 |
+downstream build, as if it had been inserted immediately after the |
|
| 461 |
+`FROM` instruction in the downstream `Dockerfile`. |
|
| 462 | 462 |
|
| 463 | 463 |
Any build instruction can be registered as a trigger. |
| 464 | 464 |
|
| ... | ... |
@@ -466,33 +465,33 @@ This is useful if you are building an image which will be used as a base |
| 466 | 466 |
to build other images, for example an application build environment or a |
| 467 | 467 |
daemon which may be customized with user-specific configuration. |
| 468 | 468 |
|
| 469 |
-For example, if your image is a reusable python application builder, it |
|
| 469 |
+For example, if your image is a reusable Python application builder, it |
|
| 470 | 470 |
will require application source code to be added in a particular |
| 471 | 471 |
directory, and it might require a build script to be called *after* |
| 472 |
-that. You can't just call *ADD* and *RUN* now, because you don't yet |
|
| 472 |
+that. You can't just call `ADD` and `RUN` now, because you don't yet |
|
| 473 | 473 |
have access to the application source code, and it will be different for |
| 474 | 474 |
each application build. You could simply provide application developers |
| 475 |
-with a boilerplate Dockerfile to copy-paste into their application, but |
|
| 475 |
+with a boilerplate `Dockerfile` to copy-paste into their application, but |
|
| 476 | 476 |
that is inefficient, error-prone and difficult to update because it |
| 477 | 477 |
mixes with application-specific code. |
| 478 | 478 |
|
| 479 |
-The solution is to use *ONBUILD* to register in advance instructions to |
|
| 479 |
+The solution is to use `ONBUILD` to register advance instructions to |
|
| 480 | 480 |
run later, during the next build stage. |
| 481 | 481 |
|
| 482 | 482 |
Here's how it works: |
| 483 | 483 |
|
| 484 |
-1. When it encounters an *ONBUILD* instruction, the builder adds a |
|
| 484 |
+1. When it encounters an `ONBUILD` instruction, the builder adds a |
|
| 485 | 485 |
trigger to the metadata of the image being built. The instruction |
| 486 | 486 |
does not otherwise affect the current build. |
| 487 | 487 |
2. At the end of the build, a list of all triggers is stored in the |
| 488 |
- image manifest, under the key *OnBuild*. They can be inspected with |
|
| 489 |
- *docker inspect*. |
|
| 488 |
+ image manifest, under the key `OnBuild`. They can be inspected with |
|
| 489 |
+ the `docker inspect` command. |
|
| 490 | 490 |
3. Later the image may be used as a base for a new build, using the |
| 491 |
- *FROM* instruction. As part of processing the *FROM* instruction, |
|
| 492 |
- the downstream builder looks for *ONBUILD* triggers, and executes |
|
| 491 |
+ `FROM` instruction. As part of processing the `FROM` instruction, |
|
| 492 |
+ the downstream builder looks for `ONBUILD` triggers, and executes |
|
| 493 | 493 |
them in the same order they were registered. If any of the triggers |
| 494 |
- fail, the *FROM* instruction is aborted which in turn causes the |
|
| 495 |
- build to fail. If all triggers succeed, the FROM instruction |
|
| 494 |
+ fail, the `FROM` instruction is aborted which in turn causes the |
|
| 495 |
+ build to fail. If all triggers succeed, the `FROM` instruction |
|
| 496 | 496 |
completes and the build continues as usual. |
| 497 | 497 |
4. Triggers are cleared from the final image after being executed. In |
| 498 | 498 |
other words they are not inherited by "grand-children" builds. |
| ... | ... |
@@ -504,9 +503,9 @@ For example you might add something like this: |
| 504 | 504 |
ONBUILD RUN /usr/local/bin/python-build --dir /app/src |
| 505 | 505 |
[...] |
| 506 | 506 |
|
| 507 |
-> **Warning**: Chaining ONBUILD instructions using ONBUILD ONBUILD isn't allowed. |
|
| 507 |
+> **Warning**: Chaining `ONBUILD` instructions using `ONBUILD ONBUILD` isn't allowed. |
|
| 508 | 508 |
|
| 509 |
-> **Warning**: ONBUILD may not trigger FROM or MAINTAINER instructions. |
|
| 509 |
+> **Warning**: The `ONBUILD` instruction may not trigger `FROM` or `MAINTAINER` instructions. |
|
| 510 | 510 |
|
| 511 | 511 |
## Dockerfile Examples |
| 512 | 512 |
|
| ... | ... |
@@ -557,3 +556,4 @@ For example you might add something like this: |
| 557 | 557 |
|
| 558 | 558 |
# You᾿ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with |
| 559 | 559 |
# /oink. |
| 560 |
+ |