github.com/mheon/docker@v0.11.2-0.20150922122814-44f47903a831/docs/reference/builder.md (about) 1 <!--[metadata]> 2 +++ 3 title = "Dockerfile reference" 4 description = "Dockerfiles use a simple DSL which allows you to automate the steps you would normally manually take to create an image." 5 keywords = ["builder, docker, Dockerfile, automation, image creation"] 6 [menu.main] 7 parent = "mn_reference" 8 +++ 9 <![end-metadata]--> 10 11 # Dockerfile reference 12 13 Docker can build images automatically by reading the instructions from a 14 `Dockerfile`. A `Dockerfile` is a text document that contains all the commands a 15 user could call on the command line to assemble an image. Using `docker build` 16 users can create an automated build that executes several command-line 17 instructions in succession. 18 19 This page describes the commands you can use in a `Dockerfile`. When you are 20 done reading this page, refer to the [`Dockerfile` Best 21 Practices](/articles/dockerfile_best-practices) for a tip-oriented guide. 22 23 ## Usage 24 25 The [`docker build`](/reference/commandline/build/) command builds an image from 26 a `Dockerfile` and a *context*. The build's context is the files at a specified 27 location `PATH` or `URL`. The `PATH` is a directory on your local filesystem. 28 The `URL` is a the location of a Git repository. 29 30 A context is processed recursively. So, a `PATH` includes any subdirectories and 31 the `URL` includes the repository and its submodules. A simple build command 32 that uses the current directory as context: 33 34 $ docker build . 35 Sending build context to Docker daemon 6.51 MB 36 ... 37 38 The build is run by the Docker daemon, not by the CLI. The first thing a build 39 process does is send the entire context (recursively) to the daemon. In most 40 cases, it's best to start with an empty directory as context and keep your 41 Dockerfile in that directory. Add only the files needed for building the 42 Dockerfile. 43 44 >**Warning**: Do not use your root directory, `/`, as the `PATH` as it causes 45 >the build to transfer the entire contents of your hard drive to the Docker 46 >daemon. 47 48 To use a file in the build context, the `Dockerfile` refers to the file with 49 an instruction, for example, a `COPY` instruction. To increase the build's 50 performance, exclude files and directories by adding a `.dockerignore` file to 51 the context directory. For information about how to [create a `.dockerignore` 52 file](#dockerignore-file) see the documentation on this page. 53 54 Traditionally, the `Dockerfile` is called `Dockerfile` and located in the root 55 of the context. You use the `-f` flag with `docker build` to point to a Dockerfile 56 anywhere in your file system. 57 58 You can specify a repository and tag at which to save the new image if 59 the build succeeds: 60 61 $ docker build -t shykes/myapp . 62 63 The Docker daemon will run your steps one-by-one, committing the result 64 to a new image if necessary, before finally outputting the ID of your 65 new image. The Docker daemon will automatically clean up the context you 66 sent. 67 68 Note that each instruction is run independently, and causes a new image 69 to be created - so `RUN cd /tmp` will not have any effect on the next 70 instructions. 71 72 Whenever possible, Docker will re-use the intermediate images, 73 accelerating `docker build` significantly (indicated by `Using cache` - 74 see the [`Dockerfile` Best Practices 75 guide](/articles/dockerfile_best-practices/#build-cache) for more information): 76 77 $ docker build -t SvenDowideit/ambassador . 78 Uploading context 10.24 kB 79 Uploading context 80 Step 1 : FROM docker-ut 81 ---> cbba202fe96b 82 Step 2 : MAINTAINER SvenDowideit@home.org.au 83 ---> Using cache 84 ---> 51182097be13 85 Step 3 : CMD env | grep _TCP= | sed 's/.*_PORT_\([0-9]*\)_TCP=tcp:\/\/\(.*\):\(.*\)/socat TCP4-LISTEN:\1,fork,reuseaddr TCP4:\2:\3 \&/' | sh && top 86 ---> Using cache 87 ---> 1a5ffc17324d 88 Successfully built 1a5ffc17324d 89 90 When you're done with your build, you're ready to look into [*Pushing a 91 repository to its registry*]( /userguide/dockerrepos/#contributing-to-docker-hub). 92 93 ## Format 94 95 Here is the format of the `Dockerfile`: 96 97 # Comment 98 INSTRUCTION arguments 99 100 The Instruction is not case-sensitive, however convention is for them to 101 be UPPERCASE in order to distinguish them from arguments more easily. 102 103 Docker runs the instructions in a `Dockerfile` in order. **The 104 first instruction must be \`FROM\`** in order to specify the [*Base 105 Image*](/reference/glossary/#base-image) from which you are building. 106 107 Docker will treat lines that *begin* with `#` as a 108 comment. A `#` marker anywhere else in the line will 109 be treated as an argument. This allows statements like: 110 111 # Comment 112 RUN echo 'we are running some # of cool things' 113 114 Here is the set of instructions you can use in a `Dockerfile` for building 115 images. 116 117 ### Environment replacement 118 119 Environment variables (declared with [the `ENV` statement](#env)) can also be 120 used in certain instructions as variables to be interpreted by the 121 `Dockerfile`. Escapes are also handled for including variable-like syntax 122 into a statement literally. 123 124 Environment variables are notated in the `Dockerfile` either with 125 `$variable_name` or `${variable_name}`. They are treated equivalently and the 126 brace syntax is typically used to address issues with variable names with no 127 whitespace, like `${foo}_bar`. 128 129 The `${variable_name}` syntax also supports a few of the standard `bash` 130 modifiers as specified below: 131 132 * `${variable:-word}` indicates that if `variable` is set then the result 133 will be that value. If `variable` is not set then `word` will be the result. 134 * `${variable:+word}` indicates that if `variable` is set then `word` will be 135 the result, otherwise the result is the empty string. 136 137 In all cases, `word` can be any string, including additional environment 138 variables. 139 140 Escaping is possible by adding a `\` before the variable: `\$foo` or `\${foo}`, 141 for example, will translate to `$foo` and `${foo}` literals respectively. 142 143 Example (parsed representation is displayed after the `#`): 144 145 FROM busybox 146 ENV foo /bar 147 WORKDIR ${foo} # WORKDIR /bar 148 ADD . $foo # ADD . /bar 149 COPY \$foo /quux # COPY $foo /quux 150 151 Environment variables are supported by the following list of instructions in 152 the `Dockerfile`: 153 154 * `ADD` 155 * `COPY` 156 * `ENV` 157 * `EXPOSE` 158 * `USER` 159 * `WORKDIR` 160 * `VOLUME` 161 * `STOPSIGNAL` 162 163 as well as: 164 165 * `ONBUILD` (when combined with one of the supported instructions above) 166 167 > **Note**: 168 > prior to 1.4, `ONBUILD` instructions did **NOT** support environment 169 > variable, even when combined with any of the instructions listed above. 170 171 Environment variable substitution will use the same value for each variable 172 throughout the entire command. In other words, in this example: 173 174 ENV abc=hello 175 ENV abc=bye def=$abc 176 ENV ghi=$abc 177 178 will result in `def` having a value of `hello`, not `bye`. However, 179 `ghi` will have a value of `bye` because it is not part of the same command 180 that set `abc` to `bye`. 181 182 ### .dockerignore file 183 184 If a file named `.dockerignore` exists in the root of `PATH`, then Docker 185 interprets it as a newline-separated list of exclusion patterns. Docker excludes 186 files or directories relative to `PATH` that match these exclusion patterns. If 187 there are any `.dockerignore` files in `PATH` subdirectories, Docker treats 188 them as normal files. 189 190 Filepaths in `.dockerignore` are absolute with the current directory as the 191 root. Wildcards are allowed but the search is not recursive. Globbing (file name 192 expansion) is done using Go's 193 [filepath.Match](http://golang.org/pkg/path/filepath#Match) rules. 194 195 You can specify exceptions to exclusion rules. To do this, simply prefix a 196 pattern with an `!` (exclamation mark) in the same way you would in a 197 `.gitignore` file. Currently there is no support for regular expressions. 198 Formats like `[^temp*]` are ignored. 199 200 The following is an example `.dockerignore` file: 201 202 ``` 203 */temp* 204 */*/temp* 205 temp? 206 *.md 207 !LICENSE.md 208 ``` 209 210 This file causes the following build behavior: 211 212 | Rule | Behavior | 213 |----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 214 | `*/temp*` | Exclude all files with names starting with`temp` in any subdirectory below the root directory. For example, a file named`/somedir/temporary.txt` is ignored. | 215 | `*/*/temp*` | Exclude files starting with name `temp` from any subdirectory that is two levels below the root directory. For example, the file `/somedir/subdir/temporary.txt` is ignored. | 216 | `temp?` | Exclude the files that match the pattern in the root directory. For example, the files `tempa`, `tempb` in the root directory are ignored. | 217 | `*.md ` | Exclude all markdown files in the root directory. | 218 | `!LICENSE.md` | Exception to the Markdown files exclusion is this file, `LICENSE.md`, Include this file in the build. | 219 220 The placement of `!` exception rules influences the matching algorithm; the 221 last line of the `.dockerignore` that matches a particular file determines 222 whether it is included or excluded. In the above example, the `LICENSE.md` file 223 matches both the `*.md` and `!LICENSE.md` rule. If you reverse the lines in the 224 example: 225 226 ``` 227 */temp* 228 */*/temp* 229 temp? 230 !LICENSE.md 231 *.md 232 ``` 233 234 The build would exclude `LICENSE.md` because the last `*.md` rule adds all 235 Markdown files in the root directory back onto the ignore list. The 236 `!LICENSE.md` rule has no effect because the subsequent `*.md` rule overrides 237 it. 238 239 You can even use the `.dockerignore` file to ignore the `Dockerfile` and 240 `.dockerignore` files. This is useful if you are copying files from the root of 241 the build context into your new container but do not want to include the 242 `Dockerfile` or `.dockerignore` files (e.g. `ADD . /someDir/`). 243 244 245 ## FROM 246 247 FROM <image> 248 249 Or 250 251 FROM <image>:<tag> 252 253 Or 254 255 FROM <image>@<digest> 256 257 The `FROM` instruction sets the [*Base Image*](/reference/glossary/#base-image) 258 for subsequent instructions. As such, a valid `Dockerfile` must have `FROM` as 259 its first instruction. The image can be any valid image – it is especially easy 260 to start by **pulling an image** from the [*Public Repositories*]( 261 /userguide/dockerrepos). 262 263 `FROM` must be the first non-comment instruction in the `Dockerfile`. 264 265 `FROM` can appear multiple times within a single `Dockerfile` in order to create 266 multiple images. Simply make a note of the last image ID output by the commit 267 before each new `FROM` command. 268 269 The `tag` or `digest` values are optional. If you omit either of them, the builder 270 assumes a `latest` by default. The builder returns an error if it cannot match 271 the `tag` value. 272 273 ## MAINTAINER 274 275 MAINTAINER <name> 276 277 The `MAINTAINER` instruction allows you to set the *Author* field of the 278 generated images. 279 280 ## RUN 281 282 RUN has 2 forms: 283 284 - `RUN <command>` (the command is run in a shell - `/bin/sh -c` - *shell* form) 285 - `RUN ["executable", "param1", "param2"]` (*exec* form) 286 287 The `RUN` instruction will execute any commands in a new layer on top of the 288 current image and commit the results. The resulting committed image will be 289 used for the next step in the `Dockerfile`. 290 291 Layering `RUN` instructions and generating commits conforms to the core 292 concepts of Docker where commits are cheap and containers can be created from 293 any point in an image's history, much like source control. 294 295 The *exec* form makes it possible to avoid shell string munging, and to `RUN` 296 commands using a base image that does not contain `/bin/sh`. 297 298 In the *shell* form you can use a `\` (backslash) to continue a single 299 RUN instruction onto the next line. For example, consider these two lines: 300 ``` 301 RUN /bin/bash -c 'source $HOME/.bashrc ;\ 302 echo $HOME' 303 ``` 304 Together they are equivalent to this single line: 305 ``` 306 RUN /bin/bash -c 'source $HOME/.bashrc ; echo $HOME' 307 ``` 308 309 > **Note**: 310 > To use a different shell, other than '/bin/sh', use the *exec* form 311 > passing in the desired shell. For example, 312 > `RUN ["/bin/bash", "-c", "echo hello"]` 313 314 > **Note**: 315 > The *exec* form is parsed as a JSON array, which means that 316 > you must use double-quotes (") around words not single-quotes ('). 317 318 > **Note**: 319 > Unlike the *shell* form, the *exec* form does not invoke a command shell. 320 > This means that normal shell processing does not happen. For example, 321 > `RUN [ "echo", "$HOME" ]` will not do variable substitution on `$HOME`. 322 > If you want shell processing then either use the *shell* form or execute 323 > a shell directly, for example: `RUN [ "sh", "-c", "echo", "$HOME" ]`. 324 325 The cache for `RUN` instructions isn't invalidated automatically during 326 the next build. The cache for an instruction like 327 `RUN apt-get dist-upgrade -y` will be reused during the next build. The 328 cache for `RUN` instructions can be invalidated by using the `--no-cache` 329 flag, for example `docker build --no-cache`. 330 331 See the [`Dockerfile` Best Practices 332 guide](/articles/dockerfile_best-practices/#build-cache) for more information. 333 334 The cache for `RUN` instructions can be invalidated by `ADD` instructions. See 335 [below](#add) for details. 336 337 ### Known issues (RUN) 338 339 - [Issue 783](https://github.com/docker/docker/issues/783) is about file 340 permissions problems that can occur when using the AUFS file system. You 341 might notice it during an attempt to `rm` a file, for example. 342 343 For systems that have recent aufs version (i.e., `dirperm1` mount option can 344 be set), docker will attempt to fix the issue automatically by mounting 345 the layers with `dirperm1` option. More details on `dirperm1` option can be 346 found at [`aufs` man page](http://aufs.sourceforge.net/aufs3/man.html) 347 348 If your system doesn't have support for `dirperm1`, the issue describes a workaround. 349 350 ## CMD 351 352 The `CMD` instruction has three forms: 353 354 - `CMD ["executable","param1","param2"]` (*exec* form, this is the preferred form) 355 - `CMD ["param1","param2"]` (as *default parameters to ENTRYPOINT*) 356 - `CMD command param1 param2` (*shell* form) 357 358 There can only be one `CMD` instruction in a `Dockerfile`. If you list more than one `CMD` 359 then only the last `CMD` will take effect. 360 361 **The main purpose of a `CMD` is to provide defaults for an executing 362 container.** These defaults can include an executable, or they can omit 363 the executable, in which case you must specify an `ENTRYPOINT` 364 instruction as well. 365 366 > **Note**: 367 > If `CMD` is used to provide default arguments for the `ENTRYPOINT` 368 > instruction, both the `CMD` and `ENTRYPOINT` instructions should be specified 369 > with the JSON array format. 370 371 > **Note**: 372 > The *exec* form is parsed as a JSON array, which means that 373 > you must use double-quotes (") around words not single-quotes ('). 374 375 > **Note**: 376 > Unlike the *shell* form, the *exec* form does not invoke a command shell. 377 > This means that normal shell processing does not happen. For example, 378 > `CMD [ "echo", "$HOME" ]` will not do variable substitution on `$HOME`. 379 > If you want shell processing then either use the *shell* form or execute 380 > a shell directly, for example: `CMD [ "sh", "-c", "echo", "$HOME" ]`. 381 382 When used in the shell or exec formats, the `CMD` instruction sets the command 383 to be executed when running the image. 384 385 If you use the *shell* form of the `CMD`, then the `<command>` will execute in 386 `/bin/sh -c`: 387 388 FROM ubuntu 389 CMD echo "This is a test." | wc - 390 391 If you want to **run your** `<command>` **without a shell** then you must 392 express the command as a JSON array and give the full path to the executable. 393 **This array form is the preferred format of `CMD`.** Any additional parameters 394 must be individually expressed as strings in the array: 395 396 FROM ubuntu 397 CMD ["/usr/bin/wc","--help"] 398 399 If you would like your container to run the same executable every time, then 400 you should consider using `ENTRYPOINT` in combination with `CMD`. See 401 [*ENTRYPOINT*](#entrypoint). 402 403 If the user specifies arguments to `docker run` then they will override the 404 default specified in `CMD`. 405 406 > **Note**: 407 > don't confuse `RUN` with `CMD`. `RUN` actually runs a command and commits 408 > the result; `CMD` does not execute anything at build time, but specifies 409 > the intended command for the image. 410 411 ## LABEL 412 413 LABEL <key>=<value> <key>=<value> <key>=<value> ... 414 415 The `LABEL` instruction adds metadata to an image. A `LABEL` is a 416 key-value pair. To include spaces within a `LABEL` value, use quotes and 417 backslashes as you would in command-line parsing. 418 419 LABEL "com.example.vendor"="ACME Incorporated" 420 421 An image can have more than one label. To specify multiple labels, separate each 422 key-value pair with whitespace. 423 424 LABEL com.example.label-with-value="foo" 425 LABEL version="1.0" 426 LABEL description="This text illustrates \ 427 that label-values can span multiple lines." 428 429 Docker recommends combining labels in a single `LABEL` instruction where 430 possible. Each `LABEL` instruction produces a new layer which can result in an 431 inefficient image if you use many labels. This example results in four image 432 layers. 433 434 LABEL multi.label1="value1" multi.label2="value2" other="value3" 435 436 Labels are additive including `LABEL`s in `FROM` images. As the system 437 encounters and then applies a new label, new `key`s override any previous labels 438 with identical keys. 439 440 To view an image's labels, use the `docker inspect` command. 441 442 "Labels": { 443 "com.example.vendor": "ACME Incorporated" 444 "com.example.label-with-value": "foo", 445 "version": "1.0", 446 "description": "This text illustrates that label-values can span multiple lines.", 447 "multi.label1": "value1", 448 "multi.label2": "value2", 449 "other": "value3" 450 }, 451 452 ## EXPOSE 453 454 EXPOSE <port> [<port>...] 455 456 The `EXPOSE` instructions informs Docker that the container will listen on the 457 specified network ports at runtime. Docker uses this information to interconnect 458 containers using links (see the [Docker User 459 Guide](/userguide/dockerlinks)) and to determine which ports to expose to the 460 host when [using the -P flag](/reference/run/#expose-incoming-ports). 461 462 > **Note**: 463 > `EXPOSE` doesn't define which ports can be exposed to the host or make ports 464 > accessible from the host by default. To expose ports to the host, at runtime, 465 > [use the `-p` flag](/userguide/dockerlinks) or 466 > [the -P flag](/reference/run/#expose-incoming-ports). 467 468 ## ENV 469 470 ENV <key> <value> 471 ENV <key>=<value> ... 472 473 The `ENV` instruction sets the environment variable `<key>` to the value 474 `<value>`. This value will be in the environment of all "descendent" `Dockerfile` 475 commands and can be [replaced inline](#environment-replacement) in many as well. 476 477 The `ENV` instruction has two forms. The first form, `ENV <key> <value>`, 478 will set a single variable to a value. The entire string after the first 479 space will be treated as the `<value>` - including characters such as 480 spaces and quotes. 481 482 The second form, `ENV <key>=<value> ...`, allows for multiple variables to 483 be set at one time. Notice that the second form uses the equals sign (=) 484 in the syntax, while the first form does not. Like command line parsing, 485 quotes and backslashes can be used to include spaces within values. 486 487 For example: 488 489 ENV myName="John Doe" myDog=Rex\ The\ Dog \ 490 myCat=fluffy 491 492 and 493 494 ENV myName John Doe 495 ENV myDog Rex The Dog 496 ENV myCat fluffy 497 498 will yield the same net results in the final container, but the first form 499 does it all in one layer. 500 501 The environment variables set using `ENV` will persist when a container is run 502 from the resulting image. You can view the values using `docker inspect`, and 503 change them using `docker run --env <key>=<value>`. 504 505 > **Note**: 506 > Environment persistence can cause unexpected effects. For example, 507 > setting `ENV DEBIAN_FRONTEND noninteractive` may confuse apt-get 508 > users on a Debian-based image. To set a value for a single command, use 509 > `RUN <key>=<value> <command>`. 510 511 ## ADD 512 513 ADD has two forms: 514 515 - `ADD <src>... <dest>` 516 - `ADD ["<src>",... "<dest>"]` (this form is required for paths containing 517 whitespace) 518 519 The `ADD` instruction copies new files, directories or remote file URLs from `<src>` 520 and adds them to the filesystem of the container at the path `<dest>`. 521 522 Multiple `<src>` resource may be specified but if they are files or 523 directories then they must be relative to the source directory that is 524 being built (the context of the build). 525 526 Each `<src>` may contain wildcards and matching will be done using Go's 527 [filepath.Match](http://golang.org/pkg/path/filepath#Match) rules. 528 For most command line uses this should act as expected, for example: 529 530 ADD hom* /mydir/ # adds all files starting with "hom" 531 ADD hom?.txt /mydir/ # ? is replaced with any single character 532 533 The `<dest>` is an absolute path, or a path relative to `WORKDIR`, into which 534 the source will be copied inside the destination container. 535 536 ADD test aDir/ # adds "test" to `WORKDIR`/aDir/ 537 538 All new files and directories are created with a UID and GID of 0. 539 540 In the case where `<src>` is a remote file URL, the destination will 541 have permissions of 600. If the remote file being retrieved has an HTTP 542 `Last-Modified` header, the timestamp from that header will be used 543 to set the `mtime` on the destination file. However, like any other file 544 processed during an `ADD`, `mtime` will not be included in the determination 545 of whether or not the file has changed and the cache should be updated. 546 547 > **Note**: 548 > If you build by passing a `Dockerfile` through STDIN (`docker 549 > build - < somefile`), there is no build context, so the `Dockerfile` 550 > can only contain a URL based `ADD` instruction. You can also pass a 551 > compressed archive through STDIN: (`docker build - < archive.tar.gz`), 552 > the `Dockerfile` at the root of the archive and the rest of the 553 > archive will get used at the context of the build. 554 555 > **Note**: 556 > If your URL files are protected using authentication, you 557 > will need to use `RUN wget`, `RUN curl` or use another tool from 558 > within the container as the `ADD` instruction does not support 559 > authentication. 560 561 > **Note**: 562 > The first encountered `ADD` instruction will invalidate the cache for all 563 > following instructions from the Dockerfile if the contents of `<src>` have 564 > changed. This includes invalidating the cache for `RUN` instructions. 565 > See the [`Dockerfile` Best Practices 566 guide](/articles/dockerfile_best-practices/#build-cache) for more information. 567 568 569 The copy obeys the following rules: 570 571 - The `<src>` path must be inside the *context* of the build; 572 you cannot `ADD ../something /something`, because the first step of a 573 `docker build` is to send the context directory (and subdirectories) to the 574 docker daemon. 575 576 - If `<src>` is a URL and `<dest>` does not end with a trailing slash, then a 577 file is downloaded from the URL and copied to `<dest>`. 578 579 - If `<src>` is a URL and `<dest>` does end with a trailing slash, then the 580 filename is inferred from the URL and the file is downloaded to 581 `<dest>/<filename>`. For instance, `ADD http://example.com/foobar /` would 582 create the file `/foobar`. The URL must have a nontrivial path so that an 583 appropriate filename can be discovered in this case (`http://example.com` 584 will not work). 585 586 - If `<src>` is a directory, the entire contents of the directory are copied, 587 including filesystem metadata. 588 > **Note**: 589 > The directory itself is not copied, just its contents. 590 591 - If `<src>` is a *local* tar archive in a recognized compression format 592 (identity, gzip, bzip2 or xz) then it is unpacked as a directory. Resources 593 from *remote* URLs are **not** decompressed. When a directory is copied or 594 unpacked, it has the same behavior as `tar -x`: the result is the union of: 595 596 1. Whatever existed at the destination path and 597 2. The contents of the source tree, with conflicts resolved in favor 598 of "2." on a file-by-file basis. 599 600 - If `<src>` is any other kind of file, it is copied individually along with 601 its metadata. In this case, if `<dest>` ends with a trailing slash `/`, it 602 will be considered a directory and the contents of `<src>` will be written 603 at `<dest>/base(<src>)`. 604 605 - If multiple `<src>` resources are specified, either directly or due to the 606 use of a wildcard, then `<dest>` must be a directory, and it must end with 607 a slash `/`. 608 609 - If `<dest>` does not end with a trailing slash, it will be considered a 610 regular file and the contents of `<src>` will be written at `<dest>`. 611 612 - If `<dest>` doesn't exist, it is created along with all missing directories 613 in its path. 614 615 ## COPY 616 617 COPY has two forms: 618 619 - `COPY <src>... <dest>` 620 - `COPY ["<src>",... "<dest>"]` (this form is required for paths containing 621 whitespace) 622 623 The `COPY` instruction copies new files or directories from `<src>` 624 and adds them to the filesystem of the container at the path `<dest>`. 625 626 Multiple `<src>` resource may be specified but they must be relative 627 to the source directory that is being built (the context of the build). 628 629 Each `<src>` may contain wildcards and matching will be done using Go's 630 [filepath.Match](http://golang.org/pkg/path/filepath#Match) rules. 631 For most command line uses this should act as expected, for example: 632 633 COPY hom* /mydir/ # adds all files starting with "hom" 634 COPY hom?.txt /mydir/ # ? is replaced with any single character 635 636 The `<dest>` is an absolute path, or a path relative to `WORKDIR`, into which 637 the source will be copied inside the destination container. 638 639 COPY test aDir/ # adds "test" to `WORKDIR`/aDir/ 640 641 All new files and directories are created with a UID and GID of 0. 642 643 > **Note**: 644 > If you build using STDIN (`docker build - < somefile`), there is no 645 > build context, so `COPY` can't be used. 646 647 The copy obeys the following rules: 648 649 - The `<src>` path must be inside the *context* of the build; 650 you cannot `COPY ../something /something`, because the first step of a 651 `docker build` is to send the context directory (and subdirectories) to the 652 docker daemon. 653 654 - If `<src>` is a directory, the entire contents of the directory are copied, 655 including filesystem metadata. 656 > **Note**: 657 > The directory itself is not copied, just its contents. 658 659 - If `<src>` is any other kind of file, it is copied individually along with 660 its metadata. In this case, if `<dest>` ends with a trailing slash `/`, it 661 will be considered a directory and the contents of `<src>` will be written 662 at `<dest>/base(<src>)`. 663 664 - If multiple `<src>` resources are specified, either directly or due to the 665 use of a wildcard, then `<dest>` must be a directory, and it must end with 666 a slash `/`. 667 668 - If `<dest>` does not end with a trailing slash, it will be considered a 669 regular file and the contents of `<src>` will be written at `<dest>`. 670 671 - If `<dest>` doesn't exist, it is created along with all missing directories 672 in its path. 673 674 ## ENTRYPOINT 675 676 ENTRYPOINT has two forms: 677 678 - `ENTRYPOINT ["executable", "param1", "param2"]` 679 (the preferred *exec* form) 680 - `ENTRYPOINT command param1 param2` 681 (*shell* form) 682 683 An `ENTRYPOINT` allows you to configure a container that will run as an executable. 684 685 For example, the following will start nginx with its default content, listening 686 on port 80: 687 688 docker run -i -t --rm -p 80:80 nginx 689 690 Command line arguments to `docker run <image>` will be appended after all 691 elements in an *exec* form `ENTRYPOINT`, and will override all elements specified 692 using `CMD`. 693 This allows arguments to be passed to the entry point, i.e., `docker run <image> -d` 694 will pass the `-d` argument to the entry point. 695 You can override the `ENTRYPOINT` instruction using the `docker run --entrypoint` 696 flag. 697 698 The *shell* form prevents any `CMD` or `run` command line arguments from being 699 used, but has the disadvantage that your `ENTRYPOINT` will be started as a 700 subcommand of `/bin/sh -c`, which does not pass signals. 701 This means that the executable will not be the container's `PID 1` - and 702 will _not_ receive Unix signals - so your executable will not receive a 703 `SIGTERM` from `docker stop <container>`. 704 705 Only the last `ENTRYPOINT` instruction in the `Dockerfile` will have an effect. 706 707 ### Exec form ENTRYPOINT example 708 709 You can use the *exec* form of `ENTRYPOINT` to set fairly stable default commands 710 and arguments and then use either form of `CMD` to set additional defaults that 711 are more likely to be changed. 712 713 FROM ubuntu 714 ENTRYPOINT ["top", "-b"] 715 CMD ["-c"] 716 717 When you run the container, you can see that `top` is the only process: 718 719 $ docker run -it --rm --name test top -H 720 top - 08:25:00 up 7:27, 0 users, load average: 0.00, 0.01, 0.05 721 Threads: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie 722 %Cpu(s): 0.1 us, 0.1 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st 723 KiB Mem: 2056668 total, 1616832 used, 439836 free, 99352 buffers 724 KiB Swap: 1441840 total, 0 used, 1441840 free. 1324440 cached Mem 725 726 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 727 1 root 20 0 19744 2336 2080 R 0.0 0.1 0:00.04 top 728 729 To examine the result further, you can use `docker exec`: 730 731 $ docker exec -it test ps aux 732 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND 733 root 1 2.6 0.1 19752 2352 ? Ss+ 08:24 0:00 top -b -H 734 root 7 0.0 0.1 15572 2164 ? R+ 08:25 0:00 ps aux 735 736 And you can gracefully request `top` to shut down using `docker stop test`. 737 738 The following `Dockerfile` shows using the `ENTRYPOINT` to run Apache in the 739 foreground (i.e., as `PID 1`): 740 741 ``` 742 FROM debian:stable 743 RUN apt-get update && apt-get install -y --force-yes apache2 744 EXPOSE 80 443 745 VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"] 746 ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"] 747 ``` 748 749 If you need to write a starter script for a single executable, you can ensure that 750 the final executable receives the Unix signals by using `exec` and `gosu` 751 commands: 752 753 ```bash 754 #!/bin/bash 755 set -e 756 757 if [ "$1" = 'postgres' ]; then 758 chown -R postgres "$PGDATA" 759 760 if [ -z "$(ls -A "$PGDATA")" ]; then 761 gosu postgres initdb 762 fi 763 764 exec gosu postgres "$@" 765 fi 766 767 exec "$@" 768 ``` 769 770 Lastly, if you need to do some extra cleanup (or communicate with other containers) 771 on shutdown, or are co-ordinating more than one executable, you may need to ensure 772 that the `ENTRYPOINT` script receives the Unix signals, passes them on, and then 773 does some more work: 774 775 ``` 776 #!/bin/sh 777 # Note: I've written this using sh so it works in the busybox container too 778 779 # USE the trap if you need to also do manual cleanup after the service is stopped, 780 # or need to start multiple services in the one container 781 trap "echo TRAPed signal" HUP INT QUIT KILL TERM 782 783 # start service in background here 784 /usr/sbin/apachectl start 785 786 echo "[hit enter key to exit] or run 'docker stop <container>'" 787 read 788 789 # stop service and clean up here 790 echo "stopping apache" 791 /usr/sbin/apachectl stop 792 793 echo "exited $0" 794 ``` 795 796 If you run this image with `docker run -it --rm -p 80:80 --name test apache`, 797 you can then examine the container's processes with `docker exec`, or `docker top`, 798 and then ask the script to stop Apache: 799 800 ```bash 801 $ docker exec -it test ps aux 802 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND 803 root 1 0.1 0.0 4448 692 ? Ss+ 00:42 0:00 /bin/sh /run.sh 123 cmd cmd2 804 root 19 0.0 0.2 71304 4440 ? Ss 00:42 0:00 /usr/sbin/apache2 -k start 805 www-data 20 0.2 0.2 360468 6004 ? Sl 00:42 0:00 /usr/sbin/apache2 -k start 806 www-data 21 0.2 0.2 360468 6000 ? Sl 00:42 0:00 /usr/sbin/apache2 -k start 807 root 81 0.0 0.1 15572 2140 ? R+ 00:44 0:00 ps aux 808 $ docker top test 809 PID USER COMMAND 810 10035 root {run.sh} /bin/sh /run.sh 123 cmd cmd2 811 10054 root /usr/sbin/apache2 -k start 812 10055 33 /usr/sbin/apache2 -k start 813 10056 33 /usr/sbin/apache2 -k start 814 $ /usr/bin/time docker stop test 815 test 816 real 0m 0.27s 817 user 0m 0.03s 818 sys 0m 0.03s 819 ``` 820 821 > **Note:** you can over ride the `ENTRYPOINT` setting using `--entrypoint`, 822 > but this can only set the binary to *exec* (no `sh -c` will be used). 823 824 > **Note**: 825 > The *exec* form is parsed as a JSON array, which means that 826 > you must use double-quotes (") around words not single-quotes ('). 827 828 > **Note**: 829 > Unlike the *shell* form, the *exec* form does not invoke a command shell. 830 > This means that normal shell processing does not happen. For example, 831 > `ENTRYPOINT [ "echo", "$HOME" ]` will not do variable substitution on `$HOME`. 832 > If you want shell processing then either use the *shell* form or execute 833 > a shell directly, for example: `ENTRYPOINT [ "sh", "-c", "echo", "$HOME" ]`. 834 > Variables that are defined in the `Dockerfile`using `ENV`, will be substituted by 835 > the `Dockerfile` parser. 836 837 ### Shell form ENTRYPOINT example 838 839 You can specify a plain string for the `ENTRYPOINT` and it will execute in `/bin/sh -c`. 840 This form will use shell processing to substitute shell environment variables, 841 and will ignore any `CMD` or `docker run` command line arguments. 842 To ensure that `docker stop` will signal any long running `ENTRYPOINT` executable 843 correctly, you need to remember to start it with `exec`: 844 845 FROM ubuntu 846 ENTRYPOINT exec top -b 847 848 When you run this image, you'll see the single `PID 1` process: 849 850 $ docker run -it --rm --name test top 851 Mem: 1704520K used, 352148K free, 0K shrd, 0K buff, 140368121167873K cached 852 CPU: 5% usr 0% sys 0% nic 94% idle 0% io 0% irq 0% sirq 853 Load average: 0.08 0.03 0.05 2/98 6 854 PID PPID USER STAT VSZ %VSZ %CPU COMMAND 855 1 0 root R 3164 0% 0% top -b 856 857 Which will exit cleanly on `docker stop`: 858 859 $ /usr/bin/time docker stop test 860 test 861 real 0m 0.20s 862 user 0m 0.02s 863 sys 0m 0.04s 864 865 If you forget to add `exec` to the beginning of your `ENTRYPOINT`: 866 867 FROM ubuntu 868 ENTRYPOINT top -b 869 CMD --ignored-param1 870 871 You can then run it (giving it a name for the next step): 872 873 $ docker run -it --name test top --ignored-param2 874 Mem: 1704184K used, 352484K free, 0K shrd, 0K buff, 140621524238337K cached 875 CPU: 9% usr 2% sys 0% nic 88% idle 0% io 0% irq 0% sirq 876 Load average: 0.01 0.02 0.05 2/101 7 877 PID PPID USER STAT VSZ %VSZ %CPU COMMAND 878 1 0 root S 3168 0% 0% /bin/sh -c top -b cmd cmd2 879 7 1 root R 3164 0% 0% top -b 880 881 You can see from the output of `top` that the specified `ENTRYPOINT` is not `PID 1`. 882 883 If you then run `docker stop test`, the container will not exit cleanly - the 884 `stop` command will be forced to send a `SIGKILL` after the timeout: 885 886 $ docker exec -it test ps aux 887 PID USER COMMAND 888 1 root /bin/sh -c top -b cmd cmd2 889 7 root top -b 890 8 root ps aux 891 $ /usr/bin/time docker stop test 892 test 893 real 0m 10.19s 894 user 0m 0.04s 895 sys 0m 0.03s 896 897 ## VOLUME 898 899 VOLUME ["/data"] 900 901 The `VOLUME` instruction creates a mount point with the specified name 902 and marks it as holding externally mounted volumes from native host or other 903 containers. The value can be a JSON array, `VOLUME ["/var/log/"]`, or a plain 904 string with multiple arguments, such as `VOLUME /var/log` or `VOLUME /var/log 905 /var/db`. For more information/examples and mounting instructions via the 906 Docker client, refer to 907 [*Share Directories via Volumes*](/userguide/dockervolumes/#mount-a-host-directory-as-a-data-volume) 908 documentation. 909 910 The `docker run` command initializes the newly created volume with any data 911 that exists at the specified location within the base image. For example, 912 consider the following Dockerfile snippet: 913 914 FROM ubuntu 915 RUN mkdir /myvol 916 RUN echo "hello world" > /myvol/greeting 917 VOLUME /myvol 918 919 This Dockerfile results in an image that causes `docker run`, to 920 create a new mount point at `/myvol` and copy the `greeting` file 921 into the newly created volume. 922 923 > **Note**: 924 > If any build steps change the data within the volume after it has been 925 > declared, those changes will be discarded. 926 927 > **Note**: 928 > The list is parsed as a JSON array, which means that 929 > you must use double-quotes (") around words not single-quotes ('). 930 931 ## USER 932 933 USER daemon 934 935 The `USER` instruction sets the user name or UID to use when running the image 936 and for any `RUN`, `CMD` and `ENTRYPOINT` instructions that follow it in the 937 `Dockerfile`. 938 939 ## WORKDIR 940 941 WORKDIR /path/to/workdir 942 943 The `WORKDIR` instruction sets the working directory for any `RUN`, `CMD`, 944 `ENTRYPOINT`, `COPY` and `ADD` instructions that follow it in the `Dockerfile`. 945 946 It can be used multiple times in the one `Dockerfile`. If a relative path 947 is provided, it will be relative to the path of the previous `WORKDIR` 948 instruction. For example: 949 950 WORKDIR /a 951 WORKDIR b 952 WORKDIR c 953 RUN pwd 954 955 The output of the final `pwd` command in this `Dockerfile` would be 956 `/a/b/c`. 957 958 The `WORKDIR` instruction can resolve environment variables previously set using 959 `ENV`. You can only use environment variables explicitly set in the `Dockerfile`. 960 For example: 961 962 ENV DIRPATH /path 963 WORKDIR $DIRPATH/$DIRNAME 964 RUN pwd 965 966 The output of the final `pwd` command in this `Dockerfile` would be 967 `/path/$DIRNAME` 968 969 ## ARG 970 971 ARG <name>[=<default value>] 972 973 The `ARG` instruction defines a variable that users can pass at build-time to 974 the builder with the `docker build` command using the `--build-arg 975 <varname>=<value>` flag. If a user specifies a build argument that was not 976 defined in the Dockerfile, the build outputs an error. 977 978 ``` 979 One or more build-args were not consumed, failing build. 980 ``` 981 982 The Dockerfile author can define a single variable by specifying `ARG` once or many 983 variables by specifying `ARG` more than once. For example, a valid Dockerfile: 984 985 ``` 986 FROM busybox 987 ARG user1 988 ARG buildno 989 ... 990 ``` 991 992 A Dockerfile author may optionally specify a default value for an `ARG` instruction: 993 994 ``` 995 FROM busybox 996 ARG user1=someuser 997 ARG buildno=1 998 ... 999 ``` 1000 1001 If an `ARG` value has a default and if there is no value passed at build-time, the 1002 builder uses the default. 1003 1004 An `ARG` variable definition comes into effect from the line on which it is 1005 defined in the `Dockerfile` not from the argument's use on the command-line or 1006 elsewhere. For example, consider this Dockerfile: 1007 1008 ``` 1009 1 FROM busybox 1010 2 USER ${user:-some_user} 1011 3 ARG user 1012 4 USER $user 1013 ... 1014 ``` 1015 A user builds this file by calling: 1016 1017 ``` 1018 $ docker build --build-arg user=what_user Dockerfile 1019 ``` 1020 1021 The `USER` at line 2 evaluates to `some_user` as the `user` variable is defined on the 1022 subsequent line 3. The `USER` at line 4 evaluates to `what_user` as `user` is 1023 defined and the `what_user` value was passed on the command line. Prior to its definition by an 1024 `ARG` instruction, any use of a variable results in an empty string. 1025 1026 > **Note:** It is not recommended to use build-time variables for 1027 > passing secrets like github keys, user credentials etc. 1028 1029 You can use an `ARG` or an `ENV` instruction to specify variables that are 1030 available to the `RUN` instruction. Environment variables defined using the 1031 `ENV` instruction always override an `ARG` instruction of the same name. Consider 1032 this Dockerfile with an `ENV` and `ARG` instruction. 1033 1034 ``` 1035 1 FROM ubuntu 1036 2 ARG CONT_IMG_VER 1037 3 ENV CONT_IMG_VER v1.0.0 1038 4 RUN echo $CONT_IMG_VER 1039 ``` 1040 Then, assume this image is built with this command: 1041 1042 ``` 1043 $ docker build --build-arg CONT_IMG_VER=v2.0.1 Dockerfile 1044 ``` 1045 1046 In this case, the `RUN` instruction uses `v1.0.0` instead of the `ARG` setting 1047 passed by the user:`v2.0.1` This behavior is similar to a shell 1048 script where a locally scoped variable overrides the variables passed as 1049 arguments or inherited from environment, from its point of definition. 1050 1051 Using the example above but a different `ENV` specification you can create more 1052 useful interactions between `ARG` and `ENV` instructions: 1053 1054 ``` 1055 1 FROM ubuntu 1056 2 ARG CONT_IMG_VER 1057 3 ENV CONT_IMG_VER ${CONT_IMG_VER:-v1.0.0} 1058 4 RUN echo $CONT_IMG_VER 1059 ``` 1060 1061 Unlike an `ARG` instruction, `ENV` values are always persisted in the built 1062 image. Consider a docker build without the --build-arg flag: 1063 1064 ``` 1065 $ docker build Dockerfile 1066 ``` 1067 1068 Using this Dockerfile example, `CONT_IMG_VER` is still persisted in the image but 1069 its value would be `v1.0.0` as it is the default set in line 3 by the `ENV` instruction. 1070 1071 The variable expansion technique in this example allows you to pass arguments 1072 from the command line and persist them in the final image by leveraging the 1073 `ENV` instruction. Variable expansion is only supported for [a limited set of 1074 Dockerfile instructions.](#environment-replacement) 1075 1076 Docker has a set of predefined `ARG` variables that you can use without a 1077 corresponding `ARG` instruction in the Dockerfile. 1078 1079 * `HTTP_PROXY` 1080 * `http_proxy` 1081 * `HTTPS_PROXY` 1082 * `https_proxy` 1083 * `FTP_PROXY` 1084 * `ftp_proxy` 1085 * `NO_PROXY` 1086 * `no_proxy` 1087 1088 To use these, simply pass them on the command line using the `--build-arg 1089 <varname>=<value>` flag. 1090 1091 ## ONBUILD 1092 1093 ONBUILD [INSTRUCTION] 1094 1095 The `ONBUILD` instruction adds to the image a *trigger* instruction to 1096 be executed at a later time, when the image is used as the base for 1097 another build. The trigger will be executed in the context of the 1098 downstream build, as if it had been inserted immediately after the 1099 `FROM` instruction in the downstream `Dockerfile`. 1100 1101 Any build instruction can be registered as a trigger. 1102 1103 This is useful if you are building an image which will be used as a base 1104 to build other images, for example an application build environment or a 1105 daemon which may be customized with user-specific configuration. 1106 1107 For example, if your image is a reusable Python application builder, it 1108 will require application source code to be added in a particular 1109 directory, and it might require a build script to be called *after* 1110 that. You can't just call `ADD` and `RUN` now, because you don't yet 1111 have access to the application source code, and it will be different for 1112 each application build. You could simply provide application developers 1113 with a boilerplate `Dockerfile` to copy-paste into their application, but 1114 that is inefficient, error-prone and difficult to update because it 1115 mixes with application-specific code. 1116 1117 The solution is to use `ONBUILD` to register advance instructions to 1118 run later, during the next build stage. 1119 1120 Here's how it works: 1121 1122 1. When it encounters an `ONBUILD` instruction, the builder adds a 1123 trigger to the metadata of the image being built. The instruction 1124 does not otherwise affect the current build. 1125 2. At the end of the build, a list of all triggers is stored in the 1126 image manifest, under the key `OnBuild`. They can be inspected with 1127 the `docker inspect` command. 1128 3. Later the image may be used as a base for a new build, using the 1129 `FROM` instruction. As part of processing the `FROM` instruction, 1130 the downstream builder looks for `ONBUILD` triggers, and executes 1131 them in the same order they were registered. If any of the triggers 1132 fail, the `FROM` instruction is aborted which in turn causes the 1133 build to fail. If all triggers succeed, the `FROM` instruction 1134 completes and the build continues as usual. 1135 4. Triggers are cleared from the final image after being executed. In 1136 other words they are not inherited by "grand-children" builds. 1137 1138 For example you might add something like this: 1139 1140 [...] 1141 ONBUILD ADD . /app/src 1142 ONBUILD RUN /usr/local/bin/python-build --dir /app/src 1143 [...] 1144 1145 > **Warning**: Chaining `ONBUILD` instructions using `ONBUILD ONBUILD` isn't allowed. 1146 1147 > **Warning**: The `ONBUILD` instruction may not trigger `FROM` or `MAINTAINER` instructions. 1148 1149 ## STOPSIGNAL 1150 1151 STOPSIGNAL signal 1152 1153 The `STOPSIGNAL` instruction sets the system call signal that will be sent to the container to exit. 1154 This signal can be a valid unsigned number that matches a position in the kernel's syscall table, for instance 9, 1155 or a signal name in the format SIGNAME, for instance SIGKILL. 1156 1157 ## Dockerfile examples 1158 1159 # Nginx 1160 # 1161 # VERSION 0.0.1 1162 1163 FROM ubuntu 1164 MAINTAINER Victor Vieux <victor@docker.com> 1165 1166 LABEL Description="This image is used to start the foobar executable" Vendor="ACME Products" Version="1.0" 1167 RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server 1168 1169 # Firefox over VNC 1170 # 1171 # VERSION 0.3 1172 1173 FROM ubuntu 1174 1175 # Install vnc, xvfb in order to create a 'fake' display and firefox 1176 RUN apt-get update && apt-get install -y x11vnc xvfb firefox 1177 RUN mkdir ~/.vnc 1178 # Setup a password 1179 RUN x11vnc -storepasswd 1234 ~/.vnc/passwd 1180 # Autostart firefox (might not be the best way, but it does the trick) 1181 RUN bash -c 'echo "firefox" >> /.bashrc' 1182 1183 EXPOSE 5900 1184 CMD ["x11vnc", "-forever", "-usepw", "-create"] 1185 1186 # Multiple images example 1187 # 1188 # VERSION 0.1 1189 1190 FROM ubuntu 1191 RUN echo foo > bar 1192 # Will output something like ===> 907ad6c2736f 1193 1194 FROM ubuntu 1195 RUN echo moo > oink 1196 # Will output something like ===> 695d7793cbe4 1197 1198 # You᾿ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with 1199 # /oink. 1200