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