github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/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  
   162  as well as:
   163  
   164  * `ONBUILD` (when combined with one of the supported instructions above)
   165  
   166  > **Note**:
   167  > prior to 1.4, `ONBUILD` instructions did **NOT** support environment 
   168  > variable, even when combined with any of the instructions listed above.
   169  
   170  Environment variable substitution will use the same value for each variable
   171  throughout the entire command. In other words, in this example:
   172  
   173      ENV abc=hello
   174      ENV abc=bye def=$abc
   175      ENV ghi=$abc
   176  
   177  will result in `def` having a value of `hello`, not `bye`. However, 
   178  `ghi` will have a value of `bye` because it is not part of the same command
   179  that set `abc` to `bye`.
   180  
   181  ### .dockerignore file
   182  
   183  If a file named `.dockerignore` exists in the root of `PATH`, then Docker
   184  interprets it as a newline-separated list of exclusion patterns. Docker excludes
   185  files or directories relative to `PATH` that match these exclusion patterns. If
   186  there are any `.dockerignore` files in `PATH` subdirectories, Docker treats
   187  them as normal files. 
   188  
   189  Filepaths in `.dockerignore` are absolute with the current directory as the
   190  root. Wildcards are allowed but the search is not recursive. Globbing (file name
   191  expansion) is done using Go's
   192  [filepath.Match](http://golang.org/pkg/path/filepath#Match) rules.
   193  
   194  You can specify exceptions to exclusion rules. To do this, simply prefix a
   195  pattern with an `!` (exclamation mark) in the same way you would in a
   196  `.gitignore` file. Currently there is no support for regular expressions.
   197  Formats like `[^temp*]` are ignored. 
   198  
   199  The following is an example `.dockerignore` file:
   200  
   201  ```
   202      */temp*
   203      */*/temp*
   204      temp?
   205      *.md
   206      !LICENSE.md
   207  ```
   208  
   209  This file causes the following build behavior:
   210  
   211  | Rule           | Behavior                                                                                                                                                                     |
   212  |----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
   213  | `*/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.                 |
   214  | `*/*/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. |
   215  | `temp?`        | Exclude the files that match the pattern in the root directory. For example, the files `tempa`, `tempb` in the root directory are ignored.                                   |
   216  | `*.md `        | Exclude all markdown files in the root directory.                                                                                                                            |
   217  | `!LICENSE.md`  | Exception to the Markdown files exclusion is this file,  `LICENSE.md`, Include this file in the build.                                                                       |
   218  
   219  The placement of  `!` exception rules influences the matching algorithm; the
   220  last line of the `.dockerignore` that matches a particular file determines
   221  whether it is included or excluded. In the above example, the `LICENSE.md` file
   222  matches both the  `*.md` and `!LICENSE.md` rule. If you reverse the lines in the
   223  example:
   224  
   225  ```
   226      */temp*
   227      */*/temp*
   228      temp?
   229      !LICENSE.md
   230      *.md
   231  ```
   232  
   233  The build would exclude `LICENSE.md` because the last `*.md` rule adds all
   234  Markdown files in the root directory back onto the ignore list. The
   235  `!LICENSE.md` rule has no effect because the subsequent `*.md` rule overrides
   236  it.
   237  
   238  You can even use the  `.dockerignore` file to ignore the `Dockerfile` and
   239  `.dockerignore` files. This is useful if you are copying files from the root of
   240  the build context into your new container but do not want to include the
   241  `Dockerfile` or `.dockerignore` files (e.g. `ADD . /someDir/`).
   242  
   243  
   244  ## FROM
   245  
   246      FROM <image>
   247  
   248  Or
   249  
   250      FROM <image>:<tag>
   251  
   252  Or
   253  
   254      FROM <image>@<digest>
   255  
   256  The `FROM` instruction sets the [*Base Image*](/reference/glossary/#base-image)
   257  for subsequent instructions. As such, a valid `Dockerfile` must have `FROM` as
   258  its first instruction. The image can be any valid image – it is especially easy
   259  to start by **pulling an image** from the [*Public Repositories*](
   260  /userguide/dockerrepos).
   261  
   262  `FROM` must be the first non-comment instruction in the `Dockerfile`.
   263  
   264  `FROM` can appear multiple times within a single `Dockerfile` in order to create
   265  multiple images. Simply make a note of the last image ID output by the commit
   266  before each new `FROM` command.
   267  
   268  The `tag` or `digest` values are optional. If you omit either of them, the builder
   269  assumes a `latest` by default. The builder returns an error if it cannot match
   270  the `tag` value.
   271  
   272  ## MAINTAINER
   273  
   274      MAINTAINER <name>
   275  
   276  The `MAINTAINER` instruction allows you to set the *Author* field of the
   277  generated images.
   278  
   279  ## RUN
   280  
   281  RUN has 2 forms:
   282  
   283  - `RUN <command>` (the command is run in a shell - `/bin/sh -c` - *shell* form)
   284  - `RUN ["executable", "param1", "param2"]` (*exec* form)
   285  
   286  The `RUN` instruction will execute any commands in a new layer on top of the
   287  current image and commit the results. The resulting committed image will be
   288  used for the next step in the `Dockerfile`.
   289  
   290  Layering `RUN` instructions and generating commits conforms to the core
   291  concepts of Docker where commits are cheap and containers can be created from
   292  any point in an image's history, much like source control.
   293  
   294  The *exec* form makes it possible to avoid shell string munging, and to `RUN`
   295  commands using a base image that does not contain `/bin/sh`.
   296  
   297  > **Note**:
   298  > To use a different shell, other than '/bin/sh', use the *exec* form
   299  > passing in the desired shell. For example,
   300  > `RUN ["/bin/bash", "-c", "echo hello"]`
   301  
   302  > **Note**:
   303  > The *exec* form is parsed as a JSON array, which means that
   304  > you must use double-quotes (") around words not single-quotes (').
   305  
   306  > **Note**:
   307  > Unlike the *shell* form, the *exec* form does not invoke a command shell.
   308  > This means that normal shell processing does not happen. For example,
   309  > `RUN [ "echo", "$HOME" ]` will not do variable substitution on `$HOME`.
   310  > If you want shell processing then either use the *shell* form or execute 
   311  > a shell directly, for example: `RUN [ "sh", "-c", "echo", "$HOME" ]`.
   312  
   313  The cache for `RUN` instructions isn't invalidated automatically during
   314  the next build. The cache for an instruction like 
   315  `RUN apt-get dist-upgrade -y` will be reused during the next build. The 
   316  cache for `RUN` instructions can be invalidated by using the `--no-cache` 
   317  flag, for example `docker build --no-cache`.
   318  
   319  See the [`Dockerfile` Best Practices
   320  guide](/articles/dockerfile_best-practices/#build-cache) for more information.
   321  
   322  The cache for `RUN` instructions can be invalidated by `ADD` instructions. See
   323  [below](#add) for details.
   324  
   325  ### Known issues (RUN)
   326  
   327  - [Issue 783](https://github.com/docker/docker/issues/783) is about file
   328    permissions problems that can occur when using the AUFS file system. You
   329    might notice it during an attempt to `rm` a file, for example.
   330  
   331    For systems that have recent aufs version (i.e., `dirperm1` mount option can
   332    be set), docker will attempt to fix the issue automatically by mounting
   333    the layers with `dirperm1` option. More details on `dirperm1` option can be
   334    found at [`aufs` man page](http://aufs.sourceforge.net/aufs3/man.html)
   335  
   336    If your system doesn't have support for `dirperm1`, the issue describes a workaround.
   337  
   338  ## CMD
   339  
   340  The `CMD` instruction has three forms:
   341  
   342  - `CMD ["executable","param1","param2"]` (*exec* form, this is the preferred form)
   343  - `CMD ["param1","param2"]` (as *default parameters to ENTRYPOINT*)
   344  - `CMD command param1 param2` (*shell* form)
   345  
   346  There can only be one `CMD` instruction in a `Dockerfile`. If you list more than one `CMD`
   347  then only the last `CMD` will take effect.
   348  
   349  **The main purpose of a `CMD` is to provide defaults for an executing
   350  container.** These defaults can include an executable, or they can omit
   351  the executable, in which case you must specify an `ENTRYPOINT`
   352  instruction as well.
   353  
   354  > **Note**:
   355  > If `CMD` is used to provide default arguments for the `ENTRYPOINT` 
   356  > instruction, both the `CMD` and `ENTRYPOINT` instructions should be specified 
   357  > with the JSON array format.
   358  
   359  > **Note**:
   360  > The *exec* form is parsed as a JSON array, which means that
   361  > you must use double-quotes (") around words not single-quotes (').
   362  
   363  > **Note**:
   364  > Unlike the *shell* form, the *exec* form does not invoke a command shell.
   365  > This means that normal shell processing does not happen. For example,
   366  > `CMD [ "echo", "$HOME" ]` will not do variable substitution on `$HOME`.
   367  > If you want shell processing then either use the *shell* form or execute 
   368  > a shell directly, for example: `CMD [ "sh", "-c", "echo", "$HOME" ]`.
   369  
   370  When used in the shell or exec formats, the `CMD` instruction sets the command
   371  to be executed when running the image.
   372  
   373  If you use the *shell* form of the `CMD`, then the `<command>` will execute in
   374  `/bin/sh -c`:
   375  
   376      FROM ubuntu
   377      CMD echo "This is a test." | wc -
   378  
   379  If you want to **run your** `<command>` **without a shell** then you must
   380  express the command as a JSON array and give the full path to the executable.
   381  **This array form is the preferred format of `CMD`.** Any additional parameters
   382  must be individually expressed as strings in the array:
   383  
   384      FROM ubuntu
   385      CMD ["/usr/bin/wc","--help"]
   386  
   387  If you would like your container to run the same executable every time, then
   388  you should consider using `ENTRYPOINT` in combination with `CMD`. See
   389  [*ENTRYPOINT*](#entrypoint).
   390  
   391  If the user specifies arguments to `docker run` then they will override the
   392  default specified in `CMD`.
   393  
   394  > **Note**:
   395  > don't confuse `RUN` with `CMD`. `RUN` actually runs a command and commits
   396  > the result; `CMD` does not execute anything at build time, but specifies
   397  > the intended command for the image.
   398  
   399  ## LABEL
   400  
   401      LABEL <key>=<value> <key>=<value> <key>=<value> ...
   402  
   403  The `LABEL` instruction adds metadata to an image. A `LABEL` is a
   404  key-value pair. To include spaces within a `LABEL` value, use quotes and
   405  backslashes as you would in command-line parsing.
   406  
   407      LABEL "com.example.vendor"="ACME Incorporated"
   408  
   409  An image can have more than one label. To specify multiple labels, separate each
   410  key-value pair with whitespace.
   411  
   412      LABEL com.example.label-with-value="foo"
   413      LABEL version="1.0"
   414      LABEL description="This text illustrates \
   415      that label-values can span multiple lines."
   416  
   417  Docker recommends combining labels in a single `LABEL` instruction where
   418  possible. Each `LABEL` instruction produces a new layer which can result in an
   419  inefficient image if you use many labels. This example results in four image
   420  layers. 
   421  
   422      LABEL multi.label1="value1" multi.label2="value2" other="value3"
   423      
   424  Labels are additive including `LABEL`s in `FROM` images. As the system
   425  encounters and then applies a new label, new `key`s override any previous labels
   426  with identical keys.    
   427  
   428  To view an image's labels, use the `docker inspect` command.
   429  
   430      "Labels": {
   431          "com.example.vendor": "ACME Incorporated"
   432          "com.example.label-with-value": "foo",
   433          "version": "1.0",
   434          "description": "This text illustrates that label-values can span multiple lines.",
   435          "multi.label1": "value1",
   436          "multi.label2": "value2",
   437          "other": "value3"
   438      },
   439  
   440  ## EXPOSE
   441  
   442      EXPOSE <port> [<port>...]
   443  
   444  The `EXPOSE` instructions informs Docker that the container will listen on the
   445  specified network ports at runtime. Docker uses this information to interconnect
   446  containers using links (see the [Docker User
   447  Guide](/userguide/dockerlinks)) and to determine which ports to expose to the
   448  host when [using the -P flag](/reference/run/#expose-incoming-ports).
   449  
   450  > **Note**:
   451  > `EXPOSE` doesn't define which ports can be exposed to the host or make ports
   452  > accessible from the host by default. To expose ports to the host, at runtime,
   453  > [use the `-p` flag](/userguide/dockerlinks) or
   454  > [the -P flag](/reference/run/#expose-incoming-ports).
   455  
   456  ## ENV
   457  
   458      ENV <key> <value>
   459      ENV <key>=<value> ...
   460  
   461  The `ENV` instruction sets the environment variable `<key>` to the value
   462  `<value>`. This value will be in the environment of all "descendent" `Dockerfile`
   463  commands and can be [replaced inline](#environment-replacement) in many as well.
   464  
   465  The `ENV` instruction has two forms. The first form, `ENV <key> <value>`,
   466  will set a single variable to a value. The entire string after the first
   467  space will be treated as the `<value>` - including characters such as 
   468  spaces and quotes.
   469  
   470  The second form, `ENV <key>=<value> ...`, allows for multiple variables to 
   471  be set at one time. Notice that the second form uses the equals sign (=) 
   472  in the syntax, while the first form does not. Like command line parsing, 
   473  quotes and backslashes can be used to include spaces within values.
   474  
   475  For example:
   476  
   477      ENV myName="John Doe" myDog=Rex\ The\ Dog \
   478          myCat=fluffy
   479  
   480  and
   481  
   482      ENV myName John Doe
   483      ENV myDog Rex The Dog
   484      ENV myCat fluffy
   485  
   486  will yield the same net results in the final container, but the first form 
   487  does it all in one layer.
   488  
   489  The environment variables set using `ENV` will persist when a container is run
   490  from the resulting image. You can view the values using `docker inspect`, and
   491  change them using `docker run --env <key>=<value>`.
   492  
   493  > **Note**:
   494  > Environment persistence can cause unexpected effects. For example,
   495  > setting `ENV DEBIAN_FRONTEND noninteractive` may confuse apt-get
   496  > users on a Debian-based image. To set a value for a single command, use
   497  > `RUN <key>=<value> <command>`.
   498  
   499  ## ADD
   500  
   501  ADD has two forms:
   502  
   503  - `ADD <src>... <dest>`
   504  - `ADD ["<src>",... "<dest>"]` (this form is required for paths containing
   505  whitespace)
   506  
   507  The `ADD` instruction copies new files, directories or remote file URLs from `<src>`
   508  and adds them to the filesystem of the container at the path `<dest>`.  
   509  
   510  Multiple `<src>` resource may be specified but if they are files or 
   511  directories then they must be relative to the source directory that is 
   512  being built (the context of the build).
   513  
   514  Each `<src>` may contain wildcards and matching will be done using Go's
   515  [filepath.Match](http://golang.org/pkg/path/filepath#Match) rules.
   516  For most command line uses this should act as expected, for example:
   517  
   518      ADD hom* /mydir/        # adds all files starting with "hom"
   519      ADD hom?.txt /mydir/    # ? is replaced with any single character
   520  
   521  The `<dest>` is an absolute path, or a path relative to `WORKDIR`, into which
   522  the source will be copied inside the destination container.
   523  
   524      ADD test aDir/          # adds "test" to `WORKDIR`/aDir/
   525  
   526  All new files and directories are created with a UID and GID of 0.
   527  
   528  In the case where `<src>` is a remote file URL, the destination will
   529  have permissions of 600. If the remote file being retrieved has an HTTP
   530  `Last-Modified` header, the timestamp from that header will be used
   531  to set the `mtime` on the destination file. However, like any other file
   532  processed during an `ADD`, `mtime` will not be included in the determination
   533  of whether or not the file has changed and the cache should be updated.
   534  
   535  > **Note**:
   536  > If you build by passing a `Dockerfile` through STDIN (`docker
   537  > build - < somefile`), there is no build context, so the `Dockerfile`
   538  > can only contain a URL based `ADD` instruction. You can also pass a
   539  > compressed archive through STDIN: (`docker build - < archive.tar.gz`),
   540  > the `Dockerfile` at the root of the archive and the rest of the
   541  > archive will get used at the context of the build.
   542  
   543  > **Note**:
   544  > If your URL files are protected using authentication, you
   545  > will need to use `RUN wget`, `RUN curl` or use another tool from
   546  > within the container as the `ADD` instruction does not support
   547  > authentication.
   548  
   549  > **Note**:
   550  > The first encountered `ADD` instruction will invalidate the cache for all
   551  > following instructions from the Dockerfile if the contents of `<src>` have
   552  > changed. This includes invalidating the cache for `RUN` instructions.
   553  > See the [`Dockerfile` Best Practices
   554  guide](/articles/dockerfile_best-practices/#build-cache) for more information.
   555  
   556  
   557  The copy obeys the following rules:
   558  
   559  - The `<src>` path must be inside the *context* of the build;
   560    you cannot `ADD ../something /something`, because the first step of a
   561    `docker build` is to send the context directory (and subdirectories) to the
   562    docker daemon.
   563  
   564  - If `<src>` is a URL and `<dest>` does not end with a trailing slash, then a
   565    file is downloaded from the URL and copied to `<dest>`.
   566  
   567  - If `<src>` is a URL and `<dest>` does end with a trailing slash, then the
   568    filename is inferred from the URL and the file is downloaded to
   569    `<dest>/<filename>`. For instance, `ADD http://example.com/foobar /` would
   570    create the file `/foobar`. The URL must have a nontrivial path so that an
   571    appropriate filename can be discovered in this case (`http://example.com`
   572    will not work).
   573  
   574  - If `<src>` is a directory, the entire contents of the directory are copied, 
   575    including filesystem metadata. 
   576  > **Note**:
   577  > The directory itself is not copied, just its contents.
   578  
   579  - If `<src>` is a *local* tar archive in a recognized compression format
   580    (identity, gzip, bzip2 or xz) then it is unpacked as a directory. Resources
   581    from *remote* URLs are **not** decompressed. When a directory is copied or
   582    unpacked, it has the same behavior as `tar -x`: the result is the union of:
   583  
   584      1. Whatever existed at the destination path and
   585      2. The contents of the source tree, with conflicts resolved in favor
   586         of "2." on a file-by-file basis.
   587  
   588  - If `<src>` is any other kind of file, it is copied individually along with
   589    its metadata. In this case, if `<dest>` ends with a trailing slash `/`, it
   590    will be considered a directory and the contents of `<src>` will be written
   591    at `<dest>/base(<src>)`.
   592  
   593  - If multiple `<src>` resources are specified, either directly or due to the
   594    use of a wildcard, then `<dest>` must be a directory, and it must end with 
   595    a slash `/`.
   596  
   597  - If `<dest>` does not end with a trailing slash, it will be considered a
   598    regular file and the contents of `<src>` will be written at `<dest>`.
   599  
   600  - If `<dest>` doesn't exist, it is created along with all missing directories
   601    in its path.
   602  
   603  ## COPY
   604  
   605  COPY has two forms:
   606  
   607  - `COPY <src>... <dest>`
   608  - `COPY ["<src>",... "<dest>"]` (this form is required for paths containing
   609  whitespace)
   610  
   611  The `COPY` instruction copies new files or directories from `<src>`
   612  and adds them to the filesystem of the container at the path `<dest>`.
   613  
   614  Multiple `<src>` resource may be specified but they must be relative
   615  to the source directory that is being built (the context of the build).
   616  
   617  Each `<src>` may contain wildcards and matching will be done using Go's
   618  [filepath.Match](http://golang.org/pkg/path/filepath#Match) rules.
   619  For most command line uses this should act as expected, for example:
   620  
   621      COPY hom* /mydir/        # adds all files starting with "hom"
   622      COPY hom?.txt /mydir/    # ? is replaced with any single character
   623  
   624  The `<dest>` is an absolute path, or a path relative to `WORKDIR`, into which
   625  the source will be copied inside the destination container.
   626  
   627      COPY test aDir/          # adds "test" to `WORKDIR`/aDir/
   628  
   629  All new files and directories are created with a UID and GID of 0.
   630  
   631  > **Note**:
   632  > If you build using STDIN (`docker build - < somefile`), there is no
   633  > build context, so `COPY` can't be used.
   634  
   635  The copy obeys the following rules:
   636  
   637  - The `<src>` path must be inside the *context* of the build;
   638    you cannot `COPY ../something /something`, because the first step of a
   639    `docker build` is to send the context directory (and subdirectories) to the
   640    docker daemon.
   641  
   642  - If `<src>` is a directory, the entire contents of the directory are copied, 
   643    including filesystem metadata. 
   644  > **Note**:
   645  > The directory itself is not copied, just its contents.
   646  
   647  - If `<src>` is any other kind of file, it is copied individually along with
   648    its metadata. In this case, if `<dest>` ends with a trailing slash `/`, it
   649    will be considered a directory and the contents of `<src>` will be written
   650    at `<dest>/base(<src>)`.
   651  
   652  - If multiple `<src>` resources are specified, either directly or due to the
   653    use of a wildcard, then `<dest>` must be a directory, and it must end with 
   654    a slash `/`.
   655  
   656  - If `<dest>` does not end with a trailing slash, it will be considered a
   657    regular file and the contents of `<src>` will be written at `<dest>`.
   658  
   659  - If `<dest>` doesn't exist, it is created along with all missing directories
   660    in its path.
   661  
   662  ## ENTRYPOINT
   663  
   664  ENTRYPOINT has two forms:
   665  
   666  - `ENTRYPOINT ["executable", "param1", "param2"]`
   667    (the preferred *exec* form)
   668  - `ENTRYPOINT command param1 param2`
   669    (*shell* form)
   670  
   671  An `ENTRYPOINT` allows you to configure a container that will run as an executable.
   672  
   673  For example, the following will start nginx with its default content, listening
   674  on port 80:
   675  
   676      docker run -i -t --rm -p 80:80 nginx
   677  
   678  Command line arguments to `docker run <image>` will be appended after all
   679  elements in an *exec* form `ENTRYPOINT`, and will override all elements specified
   680  using `CMD`.
   681  This allows arguments to be passed to the entry point, i.e., `docker run <image> -d`
   682  will pass the `-d` argument to the entry point. 
   683  You can override the `ENTRYPOINT` instruction using the `docker run --entrypoint`
   684  flag.
   685  
   686  The *shell* form prevents any `CMD` or `run` command line arguments from being
   687  used, but has the disadvantage that your `ENTRYPOINT` will be started as a
   688  subcommand of `/bin/sh -c`, which does not pass signals.
   689  This means that the executable will not be the container's `PID 1` - and
   690  will _not_ receive Unix signals - so your executable will not receive a
   691  `SIGTERM` from `docker stop <container>`.
   692  
   693  Only the last `ENTRYPOINT` instruction in the `Dockerfile` will have an effect.
   694  
   695  ### Exec form ENTRYPOINT example
   696  
   697  You can use the *exec* form of `ENTRYPOINT` to set fairly stable default commands
   698  and arguments and then use either form of `CMD` to set additional defaults that
   699  are more likely to be changed.
   700  
   701      FROM ubuntu
   702      ENTRYPOINT ["top", "-b"]
   703      CMD ["-c"]
   704  
   705  When you run the container, you can see that `top` is the only process:
   706  
   707      $ docker run -it --rm --name test  top -H
   708      top - 08:25:00 up  7:27,  0 users,  load average: 0.00, 0.01, 0.05
   709      Threads:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
   710      %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
   711      KiB Mem:   2056668 total,  1616832 used,   439836 free,    99352 buffers
   712      KiB Swap:  1441840 total,        0 used,  1441840 free.  1324440 cached Mem
   713      
   714        PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
   715          1 root      20   0   19744   2336   2080 R  0.0  0.1   0:00.04 top
   716      
   717  To examine the result further, you can use `docker exec`:
   718  
   719      $ docker exec -it test ps aux
   720      USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
   721      root         1  2.6  0.1  19752  2352 ?        Ss+  08:24   0:00 top -b -H
   722      root         7  0.0  0.1  15572  2164 ?        R+   08:25   0:00 ps aux
   723  
   724  And you can gracefully request `top` to shut down using `docker stop test`.
   725  
   726  The following `Dockerfile` shows using the `ENTRYPOINT` to run Apache in the
   727  foreground (i.e., as `PID 1`):
   728  
   729  ```
   730  FROM debian:stable
   731  RUN apt-get update && apt-get install -y --force-yes apache2
   732  EXPOSE 80 443
   733  VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
   734  ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
   735  ```
   736  
   737  If you need to write a starter script for a single executable, you can ensure that
   738  the final executable receives the Unix signals by using `exec` and `gosu`
   739  commands:
   740  
   741  ```bash
   742  #!/bin/bash
   743  set -e
   744  
   745  if [ "$1" = 'postgres' ]; then
   746      chown -R postgres "$PGDATA"
   747  
   748      if [ -z "$(ls -A "$PGDATA")" ]; then
   749          gosu postgres initdb
   750      fi
   751  
   752      exec gosu postgres "$@"
   753  fi
   754  
   755  exec "$@"
   756  ```
   757  
   758  Lastly, if you need to do some extra cleanup (or communicate with other containers)
   759  on shutdown, or are co-ordinating more than one executable, you may need to ensure
   760  that the `ENTRYPOINT` script receives the Unix signals, passes them on, and then
   761  does some more work:
   762  
   763  ```
   764  #!/bin/sh
   765  # Note: I've written this using sh so it works in the busybox container too
   766  
   767  # USE the trap if you need to also do manual cleanup after the service is stopped,
   768  #     or need to start multiple services in the one container
   769  trap "echo TRAPed signal" HUP INT QUIT KILL TERM
   770  
   771  # start service in background here
   772  /usr/sbin/apachectl start
   773  
   774  echo "[hit enter key to exit] or run 'docker stop <container>'"
   775  read
   776  
   777  # stop service and clean up here
   778  echo "stopping apache"
   779  /usr/sbin/apachectl stop
   780  
   781  echo "exited $0"
   782  ```
   783  
   784  If you run this image with `docker run -it --rm -p 80:80 --name test apache`,
   785  you can then examine the container's processes with `docker exec`, or `docker top`,
   786  and then ask the script to stop Apache:
   787  
   788  ```bash
   789  $ docker exec -it test ps aux
   790  USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
   791  root         1  0.1  0.0   4448   692 ?        Ss+  00:42   0:00 /bin/sh /run.sh 123 cmd cmd2
   792  root        19  0.0  0.2  71304  4440 ?        Ss   00:42   0:00 /usr/sbin/apache2 -k start
   793  www-data    20  0.2  0.2 360468  6004 ?        Sl   00:42   0:00 /usr/sbin/apache2 -k start
   794  www-data    21  0.2  0.2 360468  6000 ?        Sl   00:42   0:00 /usr/sbin/apache2 -k start
   795  root        81  0.0  0.1  15572  2140 ?        R+   00:44   0:00 ps aux
   796  $ docker top test
   797  PID                 USER                COMMAND
   798  10035               root                {run.sh} /bin/sh /run.sh 123 cmd cmd2
   799  10054               root                /usr/sbin/apache2 -k start
   800  10055               33                  /usr/sbin/apache2 -k start
   801  10056               33                  /usr/sbin/apache2 -k start
   802  $ /usr/bin/time docker stop test
   803  test
   804  real	0m 0.27s
   805  user	0m 0.03s
   806  sys	0m 0.03s
   807  ```
   808  
   809  > **Note:** you can over ride the `ENTRYPOINT` setting using `--entrypoint`,
   810  > but this can only set the binary to *exec* (no `sh -c` will be used).
   811  
   812  > **Note**:
   813  > The *exec* form is parsed as a JSON array, which means that
   814  > you must use double-quotes (") around words not single-quotes (').
   815  
   816  > **Note**:
   817  > Unlike the *shell* form, the *exec* form does not invoke a command shell.
   818  > This means that normal shell processing does not happen. For example,
   819  > `ENTRYPOINT [ "echo", "$HOME" ]` will not do variable substitution on `$HOME`.
   820  > If you want shell processing then either use the *shell* form or execute 
   821  > a shell directly, for example: `ENTRYPOINT [ "sh", "-c", "echo", "$HOME" ]`.
   822  > Variables that are defined in the `Dockerfile`using `ENV`, will be substituted by
   823  > the `Dockerfile` parser.
   824  
   825  ### Shell form ENTRYPOINT example
   826  
   827  You can specify a plain string for the `ENTRYPOINT` and it will execute in `/bin/sh -c`.
   828  This form will use shell processing to substitute shell environment variables,
   829  and will ignore any `CMD` or `docker run` command line arguments.
   830  To ensure that `docker stop` will signal any long running `ENTRYPOINT` executable
   831  correctly, you need to remember to start it with `exec`:
   832  
   833      FROM ubuntu
   834      ENTRYPOINT exec top -b
   835  
   836  When you run this image, you'll see the single `PID 1` process:
   837  
   838      $ docker run -it --rm --name test top
   839      Mem: 1704520K used, 352148K free, 0K shrd, 0K buff, 140368121167873K cached
   840      CPU:   5% usr   0% sys   0% nic  94% idle   0% io   0% irq   0% sirq
   841      Load average: 0.08 0.03 0.05 2/98 6
   842        PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
   843          1     0 root     R     3164   0%   0% top -b
   844  
   845  Which will exit cleanly on `docker stop`:
   846  
   847      $ /usr/bin/time docker stop test
   848      test
   849      real	0m 0.20s
   850      user	0m 0.02s
   851      sys	0m 0.04s
   852  
   853  If you forget to add `exec` to the beginning of your `ENTRYPOINT`:
   854  
   855      FROM ubuntu
   856      ENTRYPOINT top -b
   857      CMD --ignored-param1
   858  
   859  You can then run it (giving it a name for the next step):
   860  
   861      $ docker run -it --name test top --ignored-param2
   862      Mem: 1704184K used, 352484K free, 0K shrd, 0K buff, 140621524238337K cached
   863      CPU:   9% usr   2% sys   0% nic  88% idle   0% io   0% irq   0% sirq
   864      Load average: 0.01 0.02 0.05 2/101 7
   865        PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
   866          1     0 root     S     3168   0%   0% /bin/sh -c top -b cmd cmd2
   867          7     1 root     R     3164   0%   0% top -b
   868  
   869  You can see from the output of `top` that the specified `ENTRYPOINT` is not `PID 1`.
   870  
   871  If you then run `docker stop test`, the container will not exit cleanly - the
   872  `stop` command will be forced to send a `SIGKILL` after the timeout:
   873  
   874      $ docker exec -it test ps aux
   875      PID   USER     COMMAND
   876          1 root     /bin/sh -c top -b cmd cmd2
   877          7 root     top -b
   878          8 root     ps aux
   879      $ /usr/bin/time docker stop test
   880      test
   881      real	0m 10.19s
   882      user	0m 0.04s
   883      sys	0m 0.03s
   884  
   885  ## VOLUME
   886  
   887      VOLUME ["/data"]
   888  
   889  The `VOLUME` instruction creates a mount point with the specified name
   890  and marks it as holding externally mounted volumes from native host or other
   891  containers. The value can be a JSON array, `VOLUME ["/var/log/"]`, or a plain
   892  string with multiple arguments, such as `VOLUME /var/log` or `VOLUME /var/log
   893  /var/db`. For more information/examples and mounting instructions via the
   894  Docker client, refer to 
   895  [*Share Directories via Volumes*](/userguide/dockervolumes/#mount-a-host-directory-as-a-data-volume)
   896  documentation.
   897  
   898  The `docker run` command initializes the newly created volume with any data 
   899  that exists at the specified location within the base image. For example, 
   900  consider the following Dockerfile snippet:
   901  
   902      FROM ubuntu
   903      RUN mkdir /myvol
   904      RUN echo "hello world" > /myvol/greeting
   905      VOLUME /myvol
   906  
   907  This Dockerfile results in an image that causes `docker run`, to
   908  create a new mount point at `/myvol` and copy the  `greeting` file 
   909  into the newly created volume.
   910  
   911  > **Note**:
   912  > If any build steps change the data within the volume after it has been
   913  > declared, those changes will be discarded.
   914  
   915  > **Note**:
   916  > The list is parsed as a JSON array, which means that
   917  > you must use double-quotes (") around words not single-quotes (').
   918  
   919  ## USER
   920  
   921      USER daemon
   922  
   923  The `USER` instruction sets the user name or UID to use when running the image
   924  and for any `RUN`, `CMD` and `ENTRYPOINT` instructions that follow it in the
   925  `Dockerfile`.
   926  
   927  ## WORKDIR
   928  
   929      WORKDIR /path/to/workdir
   930  
   931  The `WORKDIR` instruction sets the working directory for any `RUN`, `CMD`,
   932  `ENTRYPOINT`, `COPY` and `ADD` instructions that follow it in the `Dockerfile`.
   933  
   934  It can be used multiple times in the one `Dockerfile`. If a relative path
   935  is provided, it will be relative to the path of the previous `WORKDIR`
   936  instruction. For example:
   937  
   938      WORKDIR /a
   939      WORKDIR b
   940      WORKDIR c
   941      RUN pwd
   942  
   943  The output of the final `pwd` command in this `Dockerfile` would be
   944  `/a/b/c`.
   945  
   946  The `WORKDIR` instruction can resolve environment variables previously set using
   947  `ENV`. You can only use environment variables explicitly set in the `Dockerfile`.
   948  For example:
   949  
   950      ENV DIRPATH /path
   951      WORKDIR $DIRPATH/$DIRNAME
   952      RUN pwd
   953  
   954  The output of the final `pwd` command in this `Dockerfile` would be
   955  `/path/$DIRNAME`
   956  
   957  ## ONBUILD
   958  
   959      ONBUILD [INSTRUCTION]
   960  
   961  The `ONBUILD` instruction adds to the image a *trigger* instruction to
   962  be executed at a later time, when the image is used as the base for
   963  another build. The trigger will be executed in the context of the
   964  downstream build, as if it had been inserted immediately after the
   965  `FROM` instruction in the downstream `Dockerfile`.
   966  
   967  Any build instruction can be registered as a trigger.
   968  
   969  This is useful if you are building an image which will be used as a base
   970  to build other images, for example an application build environment or a
   971  daemon which may be customized with user-specific configuration.
   972  
   973  For example, if your image is a reusable Python application builder, it
   974  will require application source code to be added in a particular
   975  directory, and it might require a build script to be called *after*
   976  that. You can't just call `ADD` and `RUN` now, because you don't yet
   977  have access to the application source code, and it will be different for
   978  each application build. You could simply provide application developers
   979  with a boilerplate `Dockerfile` to copy-paste into their application, but
   980  that is inefficient, error-prone and difficult to update because it
   981  mixes with application-specific code.
   982  
   983  The solution is to use `ONBUILD` to register advance instructions to
   984  run later, during the next build stage.
   985  
   986  Here's how it works:
   987  
   988  1. When it encounters an `ONBUILD` instruction, the builder adds a
   989     trigger to the metadata of the image being built. The instruction
   990     does not otherwise affect the current build.
   991  2. At the end of the build, a list of all triggers is stored in the
   992     image manifest, under the key `OnBuild`. They can be inspected with
   993     the `docker inspect` command.
   994  3. Later the image may be used as a base for a new build, using the
   995     `FROM` instruction. As part of processing the `FROM` instruction,
   996     the downstream builder looks for `ONBUILD` triggers, and executes
   997     them in the same order they were registered. If any of the triggers
   998     fail, the `FROM` instruction is aborted which in turn causes the
   999     build to fail. If all triggers succeed, the `FROM` instruction
  1000     completes and the build continues as usual.
  1001  4. Triggers are cleared from the final image after being executed. In
  1002     other words they are not inherited by "grand-children" builds.
  1003  
  1004  For example you might add something like this:
  1005  
  1006      [...]
  1007      ONBUILD ADD . /app/src
  1008      ONBUILD RUN /usr/local/bin/python-build --dir /app/src
  1009      [...]
  1010  
  1011  > **Warning**: Chaining `ONBUILD` instructions using `ONBUILD ONBUILD` isn't allowed.
  1012  
  1013  > **Warning**: The `ONBUILD` instruction may not trigger `FROM` or `MAINTAINER` instructions.
  1014  
  1015  ## Dockerfile examples
  1016  
  1017      # Nginx
  1018      #
  1019      # VERSION               0.0.1
  1020  
  1021      FROM      ubuntu
  1022      MAINTAINER Victor Vieux <victor@docker.com>
  1023  
  1024      LABEL Description="This image is used to start the foobar executable" Vendor="ACME Products" Version="1.0"
  1025      RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
  1026  
  1027      # Firefox over VNC
  1028      #
  1029      # VERSION               0.3
  1030  
  1031      FROM ubuntu
  1032  
  1033      # Install vnc, xvfb in order to create a 'fake' display and firefox
  1034      RUN apt-get update && apt-get install -y x11vnc xvfb firefox
  1035      RUN mkdir ~/.vnc
  1036      # Setup a password
  1037      RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
  1038      # Autostart firefox (might not be the best way, but it does the trick)
  1039      RUN bash -c 'echo "firefox" >> /.bashrc'
  1040  
  1041      EXPOSE 5900
  1042      CMD    ["x11vnc", "-forever", "-usepw", "-create"]
  1043  
  1044      # Multiple images example
  1045      #
  1046      # VERSION               0.1
  1047  
  1048      FROM ubuntu
  1049      RUN echo foo > bar
  1050      # Will output something like ===> 907ad6c2736f
  1051  
  1052      FROM ubuntu
  1053      RUN echo moo > oink
  1054      # Will output something like ===> 695d7793cbe4
  1055  
  1056      # You᾿ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
  1057      # /oink.
  1058