github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/docs/reference/builder.md (about)

     1  ---
     2  title: "Dockerfile reference"
     3  description: "Dockerfiles use a simple DSL which allows you to automate the steps you would normally manually take to create an image."
     4  keywords: "builder, docker, Dockerfile, automation, image creation"
     5  redirect_from:
     6  - /reference/builder/
     7  ---
     8  
     9  <!-- This file is maintained within the docker/docker Github
    10       repository at https://github.com/docker/docker/. Make all
    11       pull requests against that repo. If you see this file in
    12       another repository, consider it read-only there, as it will
    13       periodically be overwritten by the definitive file. Pull
    14       requests which include edits to this file in other repositories
    15       will be rejected.
    16  -->
    17  
    18  # Dockerfile reference
    19  
    20  Docker can build images automatically by reading the instructions from a
    21  `Dockerfile`. A `Dockerfile` is a text document that contains all the commands a
    22  user could call on the command line to assemble an image. Using `docker build`
    23  users can create an automated build that executes several command-line
    24  instructions in succession.
    25  
    26  This page describes the commands you can use in a `Dockerfile`. When you are
    27  done reading this page, refer to the [`Dockerfile` Best
    28  Practices](https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/) for a tip-oriented guide.
    29  
    30  ## Usage
    31  
    32  The [`docker build`](commandline/build.md) command builds an image from
    33  a `Dockerfile` and a *context*. The build's context is the files at a specified
    34  location `PATH` or `URL`. The `PATH` is a directory on your local filesystem.
    35  The `URL` is a Git repository location.
    36  
    37  A context is processed recursively. So, a `PATH` includes any subdirectories and
    38  the `URL` includes the repository and its submodules. A simple build command
    39  that uses the current directory as context:
    40  
    41      $ docker build .
    42      Sending build context to Docker daemon  6.51 MB
    43      ...
    44  
    45  The build is run by the Docker daemon, not by the CLI. The first thing a build
    46  process does is send the entire context (recursively) to the daemon.  In most
    47  cases, it's best to start with an empty directory as context and keep your
    48  Dockerfile in that directory. Add only the files needed for building the
    49  Dockerfile.
    50  
    51  >**Warning**: Do not use your root directory, `/`, as the `PATH` as it causes
    52  >the build to transfer the entire contents of your hard drive to the Docker
    53  >daemon.
    54  
    55  To use a file in the build context, the `Dockerfile` refers to the file specified
    56  in an instruction, for example,  a `COPY` instruction. To increase the build's
    57  performance, exclude files and directories by adding a `.dockerignore` file to
    58  the context directory.  For information about how to [create a `.dockerignore`
    59  file](#dockerignore-file) see the documentation on this page.
    60  
    61  Traditionally, the `Dockerfile` is called `Dockerfile` and located in the root
    62  of the context. You use the `-f` flag with `docker build` to point to a Dockerfile
    63  anywhere in your file system.
    64  
    65      $ docker build -f /path/to/a/Dockerfile .
    66  
    67  You can specify a repository and tag at which to save the new image if
    68  the build succeeds:
    69  
    70      $ docker build -t shykes/myapp .
    71  
    72  To tag the image into multiple repositories after the build,
    73  add multiple `-t` parameters when you run the `build` command:
    74  
    75      $ docker build -t shykes/myapp:1.0.2 -t shykes/myapp:latest .
    76  
    77  Before the Docker daemon runs the instructions in the `Dockerfile`, it performs
    78  a preliminary validation of the `Dockerfile` and returns an error if the syntax is incorrect:
    79  
    80      $ docker build -t test/myapp .
    81      Sending build context to Docker daemon 2.048 kB
    82      Error response from daemon: Unknown instruction: RUNCMD
    83  
    84  The Docker daemon runs the instructions in the `Dockerfile` one-by-one,
    85  committing the result of each instruction
    86  to a new image if necessary, before finally outputting the ID of your
    87  new image. The Docker daemon will automatically clean up the context you
    88  sent.
    89  
    90  Note that each instruction is run independently, and causes a new image
    91  to be created - so `RUN cd /tmp` will not have any effect on the next
    92  instructions.
    93  
    94  Whenever possible, Docker will re-use the intermediate images (cache),
    95  to accelerate the `docker build` process significantly. This is indicated by
    96  the `Using cache` message in the console output.
    97  (For more information, see the [Build cache section](https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#/build-cache)) in the
    98  `Dockerfile` best practices guide:
    99  
   100      $ docker build -t svendowideit/ambassador .
   101      Sending build context to Docker daemon 15.36 kB
   102      Step 1/4 : FROM alpine:3.2
   103       ---> 31f630c65071
   104      Step 2/4 : MAINTAINER SvenDowideit@home.org.au
   105       ---> Using cache
   106       ---> 2a1c91448f5f
   107      Step 3/4 : RUN apk update &&      apk add socat &&        rm -r /var/cache/
   108       ---> Using cache
   109       ---> 21ed6e7fbb73
   110      Step 4/4 : 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
   111       ---> Using cache
   112       ---> 7ea8aef582cc
   113      Successfully built 7ea8aef582cc
   114  
   115  Build cache is only used from images that have a local parent chain. This means
   116  that these images were created by previous builds or the whole chain of images
   117  was loaded with `docker load`. If you wish to use build cache of a specific
   118  image you can specify it with `--cache-from` option. Images specified with
   119  `--cache-from` do not need to have a parent chain and may be pulled from other
   120  registries.
   121  
   122  When you're done with your build, you're ready to look into [*Pushing a
   123  repository to its registry*](https://docs.docker.com/engine/tutorials/dockerrepos/#/contributing-to-docker-hub).
   124  
   125  ## Format
   126  
   127  Here is the format of the `Dockerfile`:
   128  
   129  ```Dockerfile
   130  # Comment
   131  INSTRUCTION arguments
   132  ```
   133  
   134  The instruction is not case-sensitive. However, convention is for them to
   135  be UPPERCASE to distinguish them from arguments more easily.
   136  
   137  
   138  Docker runs instructions in a `Dockerfile` in order. A `Dockerfile` **must
   139  start with a \`FROM\` instruction**. The `FROM` instruction specifies the [*Base
   140  Image*](glossary.md#base-image) from which you are building. `FROM` may only be
   141  proceeded by one or more `ARG` instructions, which declare arguments that are used
   142  in `FROM` lines in the `Dockerfile`.
   143  
   144  Docker treats lines that *begin* with `#` as a comment, unless the line is
   145  a valid [parser directive](#parser-directives). A `#` marker anywhere
   146  else in a line is treated as an argument. This allows statements like:
   147  
   148  ```Dockerfile
   149  # Comment
   150  RUN echo 'we are running some # of cool things'
   151  ```
   152  
   153  Line continuation characters are not supported in comments.
   154  
   155  ## Parser directives
   156  
   157  Parser directives are optional, and affect the way in which subsequent lines
   158  in a `Dockerfile` are handled. Parser directives do not add layers to the build,
   159  and will not be shown as a build step. Parser directives are written as a
   160  special type of comment in the form `# directive=value`. A single directive
   161  may only be used once.
   162  
   163  Once a comment, empty line or builder instruction has been processed, Docker
   164  no longer looks for parser directives. Instead it treats anything formatted
   165  as a parser directive as a comment and does not attempt to validate if it might
   166  be a parser directive. Therefore, all parser directives must be at the very
   167  top of a `Dockerfile`.
   168  
   169  Parser directives are not case-sensitive. However, convention is for them to
   170  be lowercase. Convention is also to include a blank line following any
   171  parser directives. Line continuation characters are not supported in parser
   172  directives.
   173  
   174  Due to these rules, the following examples are all invalid:
   175  
   176  Invalid due to line continuation:
   177  
   178  ```Dockerfile
   179  # direc \
   180  tive=value
   181  ```
   182  
   183  Invalid due to appearing twice:
   184  
   185  ```Dockerfile
   186  # directive=value1
   187  # directive=value2
   188  
   189  FROM ImageName
   190  ```
   191  
   192  Treated as a comment due to appearing after a builder instruction:
   193  
   194  ```Dockerfile
   195  FROM ImageName
   196  # directive=value
   197  ```
   198  
   199  Treated as a comment due to appearing after a comment which is not a parser
   200  directive:
   201  
   202  ```Dockerfile
   203  # About my dockerfile
   204  # directive=value
   205  FROM ImageName
   206  ```
   207  
   208  The unknown directive is treated as a comment due to not being recognized. In
   209  addition, the known directive is treated as a comment due to appearing after
   210  a comment which is not a parser directive.
   211  
   212  ```Dockerfile
   213  # unknowndirective=value
   214  # knowndirective=value
   215  ```
   216  
   217  Non line-breaking whitespace is permitted in a parser directive. Hence, the
   218  following lines are all treated identically:
   219  
   220  ```Dockerfile
   221  #directive=value
   222  # directive =value
   223  #	directive= value
   224  # directive = value
   225  #	  dIrEcTiVe=value
   226  ```
   227  
   228  The following parser directive is supported:
   229  
   230  * `escape`
   231  
   232  ## escape
   233  
   234      # escape=\ (backslash)
   235  
   236  Or
   237  
   238      # escape=` (backtick)
   239  
   240  The `escape` directive sets the character used to escape characters in a
   241  `Dockerfile`. If not specified, the default escape character is `\`.
   242  
   243  The escape character is used both to escape characters in a line, and to
   244  escape a newline. This allows a `Dockerfile` instruction to
   245  span multiple lines. Note that regardless of whether the `escape` parser
   246  directive is included in a `Dockerfile`, *escaping is not performed in
   247  a `RUN` command, except at the end of a line.*
   248  
   249  Setting the escape character to `` ` `` is especially useful on
   250  `Windows`, where `\` is the directory path separator. `` ` `` is consistent
   251  with [Windows PowerShell](https://technet.microsoft.com/en-us/library/hh847755.aspx).
   252  
   253  Consider the following example which would fail in a non-obvious way on
   254  `Windows`. The second `\` at the end of the second line would be interpreted as an
   255  escape for the newline, instead of a target of the escape from the first `\`.
   256  Similarly, the `\` at the end of the third line would, assuming it was actually
   257  handled as an instruction, cause it be treated as a line continuation. The result
   258  of this dockerfile is that second and third lines are considered a single
   259  instruction:
   260  
   261  ```Dockerfile
   262  FROM microsoft/nanoserver
   263  COPY testfile.txt c:\\
   264  RUN dir c:\
   265  ```
   266  
   267  Results in:
   268  
   269      PS C:\John> docker build -t cmd .
   270      Sending build context to Docker daemon 3.072 kB
   271      Step 1/2 : FROM microsoft/nanoserver
   272       ---> 22738ff49c6d
   273      Step 2/2 : COPY testfile.txt c:\RUN dir c:
   274      GetFileAttributesEx c:RUN: The system cannot find the file specified.
   275      PS C:\John>
   276  
   277  One solution to the above would be to use `/` as the target of both the `COPY`
   278  instruction, and `dir`. However, this syntax is, at best, confusing as it is not
   279  natural for paths on `Windows`, and at worst, error prone as not all commands on
   280  `Windows` support `/` as the path separator.
   281  
   282  By adding the `escape` parser directive, the following `Dockerfile` succeeds as
   283  expected with the use of natural platform semantics for file paths on `Windows`:
   284  
   285      # escape=`
   286  
   287      FROM microsoft/nanoserver
   288      COPY testfile.txt c:\
   289      RUN dir c:\
   290  
   291  Results in:
   292  
   293      PS C:\John> docker build -t succeeds --no-cache=true .
   294      Sending build context to Docker daemon 3.072 kB
   295      Step 1/3 : FROM microsoft/nanoserver
   296       ---> 22738ff49c6d
   297      Step 2/3 : COPY testfile.txt c:\
   298       ---> 96655de338de
   299      Removing intermediate container 4db9acbb1682
   300      Step 3/3 : RUN dir c:\
   301       ---> Running in a2c157f842f5
   302       Volume in drive C has no label.
   303       Volume Serial Number is 7E6D-E0F7
   304      
   305       Directory of c:\
   306      
   307      10/05/2016  05:04 PM             1,894 License.txt
   308      10/05/2016  02:22 PM    <DIR>          Program Files
   309      10/05/2016  02:14 PM    <DIR>          Program Files (x86)
   310      10/28/2016  11:18 AM                62 testfile.txt
   311      10/28/2016  11:20 AM    <DIR>          Users
   312      10/28/2016  11:20 AM    <DIR>          Windows
   313                 2 File(s)          1,956 bytes
   314                 4 Dir(s)  21,259,096,064 bytes free
   315       ---> 01c7f3bef04f
   316      Removing intermediate container a2c157f842f5
   317      Successfully built 01c7f3bef04f
   318      PS C:\John>
   319  
   320  ## Environment replacement
   321  
   322  Environment variables (declared with [the `ENV` statement](#env)) can also be
   323  used in certain instructions as variables to be interpreted by the
   324  `Dockerfile`. Escapes are also handled for including variable-like syntax
   325  into a statement literally.
   326  
   327  Environment variables are notated in the `Dockerfile` either with
   328  `$variable_name` or `${variable_name}`. They are treated equivalently and the
   329  brace syntax is typically used to address issues with variable names with no
   330  whitespace, like `${foo}_bar`.
   331  
   332  The `${variable_name}` syntax also supports a few of the standard `bash`
   333  modifiers as specified below:
   334  
   335  * `${variable:-word}` indicates that if `variable` is set then the result
   336    will be that value. If `variable` is not set then `word` will be the result.
   337  * `${variable:+word}` indicates that if `variable` is set then `word` will be
   338    the result, otherwise the result is the empty string.
   339  
   340  In all cases, `word` can be any string, including additional environment
   341  variables.
   342  
   343  Escaping is possible by adding a `\` before the variable: `\$foo` or `\${foo}`,
   344  for example, will translate to `$foo` and `${foo}` literals respectively.
   345  
   346  Example (parsed representation is displayed after the `#`):
   347  
   348      FROM busybox
   349      ENV foo /bar
   350      WORKDIR ${foo}   # WORKDIR /bar
   351      ADD . $foo       # ADD . /bar
   352      COPY \$foo /quux # COPY $foo /quux
   353  
   354  Environment variables are supported by the following list of instructions in
   355  the `Dockerfile`:
   356  
   357  * `ADD`
   358  * `COPY`
   359  * `ENV`
   360  * `EXPOSE`
   361  * `FROM`
   362  * `LABEL`
   363  * `STOPSIGNAL`
   364  * `USER`
   365  * `VOLUME`
   366  * `WORKDIR`
   367  
   368  as well as:
   369  
   370  * `ONBUILD` (when combined with one of the supported instructions above)
   371  
   372  > **Note**:
   373  > prior to 1.4, `ONBUILD` instructions did **NOT** support environment
   374  > variable, even when combined with any of the instructions listed above.
   375  
   376  Environment variable substitution will use the same value for each variable
   377  throughout the entire instruction. In other words, in this example:
   378  
   379      ENV abc=hello
   380      ENV abc=bye def=$abc
   381      ENV ghi=$abc
   382  
   383  will result in `def` having a value of `hello`, not `bye`. However,
   384  `ghi` will have a value of `bye` because it is not part of the same instruction 
   385  that set `abc` to `bye`.
   386  
   387  ## .dockerignore file
   388  
   389  Before the docker CLI sends the context to the docker daemon, it looks
   390  for a file named `.dockerignore` in the root directory of the context.
   391  If this file exists, the CLI modifies the context to exclude files and
   392  directories that match patterns in it.  This helps to avoid
   393  unnecessarily sending large or sensitive files and directories to the
   394  daemon and potentially adding them to images using `ADD` or `COPY`.
   395  
   396  The CLI interprets the `.dockerignore` file as a newline-separated
   397  list of patterns similar to the file globs of Unix shells.  For the
   398  purposes of matching, the root of the context is considered to be both
   399  the working and the root directory.  For example, the patterns
   400  `/foo/bar` and `foo/bar` both exclude a file or directory named `bar`
   401  in the `foo` subdirectory of `PATH` or in the root of the git
   402  repository located at `URL`.  Neither excludes anything else.
   403  
   404  If a line in `.dockerignore` file starts with `#` in column 1, then this line is
   405  considered as a comment and is ignored before interpreted by the CLI.
   406  
   407  Here is an example `.dockerignore` file:
   408  
   409  ```
   410  # comment
   411  */temp*
   412  */*/temp*
   413  temp?
   414  ```
   415  
   416  This file causes the following build behavior:
   417  
   418  | Rule           | Behavior                                                                                                                                                                     |
   419  |----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
   420  | `# comment`    | Ignored.                 |
   421  | `*/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`.                 |
   422  | `*/*/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. |
   423  | `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.
   424  
   425  
   426  Matching is done using Go's
   427  [filepath.Match](http://golang.org/pkg/path/filepath#Match) rules.  A
   428  preprocessing step removes leading and trailing whitespace and
   429  eliminates `.` and `..` elements using Go's
   430  [filepath.Clean](http://golang.org/pkg/path/filepath/#Clean).  Lines
   431  that are blank after preprocessing are ignored.
   432  
   433  Beyond Go's filepath.Match rules, Docker also supports a special
   434  wildcard string `**` that matches any number of directories (including
   435  zero). For example, `**/*.go` will exclude all files that end with `.go`
   436  that are found in all directories, including the root of the build context.
   437  
   438  Lines starting with `!` (exclamation mark) can be used to make exceptions
   439  to exclusions.  The following is an example `.dockerignore` file that
   440  uses this mechanism:
   441  
   442  ```
   443      *.md
   444      !README.md
   445  ```
   446  
   447  All markdown files *except* `README.md` are excluded from the context.
   448  
   449  The placement of `!` exception rules influences the behavior: the last
   450  line of the `.dockerignore` that matches a particular file determines
   451  whether it is included or excluded.  Consider the following example:
   452  
   453  ```
   454      *.md
   455      !README*.md
   456      README-secret.md
   457  ```
   458  
   459  No markdown files are included in the context except README files other than
   460  `README-secret.md`.
   461  
   462  Now consider this example:
   463  
   464  ```
   465      *.md
   466      README-secret.md
   467      !README*.md
   468  ```
   469  
   470  All of the README files are included.  The middle line has no effect because
   471  `!README*.md` matches `README-secret.md` and comes last.
   472  
   473  You can even use the `.dockerignore` file to exclude the `Dockerfile`
   474  and `.dockerignore` files.  These files are still sent to the daemon
   475  because it needs them to do its job.  But the `ADD` and `COPY` instructions
   476  do not copy them to the image.
   477  
   478  Finally, you may want to specify which files to include in the
   479  context, rather than which to exclude. To achieve this, specify `*` as
   480  the first pattern, followed by one or more `!` exception patterns.
   481  
   482  **Note**: For historical reasons, the pattern `.` is ignored.
   483  
   484  ## FROM
   485  
   486      FROM <image> [AS <name>]
   487  
   488  Or
   489  
   490      FROM <image>[:<tag>] [AS <name>]
   491  
   492  Or
   493  
   494      FROM <image>[@<digest>] [AS <name>]
   495  
   496  The `FROM` instruction initializes a new build stage and sets the 
   497  [*Base Image*](glossary.md#base-image) for subsequent instructions. As such, a 
   498  valid `Dockerfile` must start with a `FROM` instruction. The image can be
   499  any valid image – it is especially easy to start by **pulling an image** from 
   500  the [*Public Repositories*](https://docs.docker.com/engine/tutorials/dockerrepos/).
   501  
   502  - `ARG` is the only instruction that may proceed `FROM` in the `Dockerfile`. 
   503    See [Understand how ARG and FROM interact](#understand-how-arg-and-from-interact).
   504  
   505  - `FROM` can appear multiple times within a single `Dockerfile` to 
   506    create multiple images or use one build stage as a dependency for another.
   507    Simply make a note of the last image ID output by the commit before each new 
   508    `FROM` instruction. Each `FROM` instruction clears any state created by previous
   509    instructions.
   510  
   511  - Optionally a name can be given to a new build stage by adding `AS name` to the 
   512    `FROM` instruction. The name can be used in subsequent `FROM` and
   513    `COPY --from=<name|index>` instructions to refer to the image built in this stage.
   514  
   515  - The `tag` or `digest` values are optional. If you omit either of them, the 
   516    builder assumes a `latest` tag by default. The builder returns an error if it
   517    cannot find the `tag` value.
   518  
   519  ### Understand how ARG and FROM interact
   520  
   521  `FROM` instructions support variables that are declared by any `ARG` 
   522  instructions that occur before the first `FROM`.
   523  
   524  ```Dockerfile
   525  ARG  CODE_VERSION=latest
   526  FROM base:${CODE_VERSION}
   527  CMD  /code/run-app
   528  
   529  FROM extras:${CODE_VERSION}
   530  CMD  /code/run-extras
   531  ```
   532  
   533  To use the default value of an `ARG` declared before the first `FROM` use an
   534  `ARG` instruction without a value:
   535  
   536  ```Dockerfile
   537  ARG  SETTINGS=default
   538  
   539  FROM busybox
   540  ARG  SETTINGS
   541  
   542  ```
   543  
   544  ## RUN
   545  
   546  RUN has 2 forms:
   547  
   548  - `RUN <command>` (*shell* form, the command is run in a shell, which by
   549  default is `/bin/sh -c` on Linux or `cmd /S /C` on Windows)
   550  - `RUN ["executable", "param1", "param2"]` (*exec* form)
   551  
   552  The `RUN` instruction will execute any commands in a new layer on top of the
   553  current image and commit the results. The resulting committed image will be
   554  used for the next step in the `Dockerfile`.
   555  
   556  Layering `RUN` instructions and generating commits conforms to the core
   557  concepts of Docker where commits are cheap and containers can be created from
   558  any point in an image's history, much like source control.
   559  
   560  The *exec* form makes it possible to avoid shell string munging, and to `RUN`
   561  commands using a base image that does not contain the specified shell executable.
   562  
   563  The default shell for the *shell* form can be changed using the `SHELL`
   564  command.
   565  
   566  In the *shell* form you can use a `\` (backslash) to continue a single
   567  RUN instruction onto the next line. For example, consider these two lines:
   568  
   569  ```
   570  RUN /bin/bash -c 'source $HOME/.bashrc; \
   571  echo $HOME'
   572  ```
   573  Together they are equivalent to this single line:
   574  
   575  ```
   576  RUN /bin/bash -c 'source $HOME/.bashrc; echo $HOME'
   577  ```
   578  
   579  > **Note**:
   580  > To use a different shell, other than '/bin/sh', use the *exec* form
   581  > passing in the desired shell. For example,
   582  > `RUN ["/bin/bash", "-c", "echo hello"]`
   583  
   584  > **Note**:
   585  > The *exec* form is parsed as a JSON array, which means that
   586  > you must use double-quotes (") around words not single-quotes (').
   587  
   588  > **Note**:
   589  > Unlike the *shell* form, the *exec* form does not invoke a command shell.
   590  > This means that normal shell processing does not happen. For example,
   591  > `RUN [ "echo", "$HOME" ]` will not do variable substitution on `$HOME`.
   592  > If you want shell processing then either use the *shell* form or execute
   593  > a shell directly, for example: `RUN [ "sh", "-c", "echo $HOME" ]`.
   594  > When using the exec form and executing a shell directly, as in the case for
   595  > the shell form, it is the shell that is doing the environment variable
   596  > expansion, not docker.
   597  >
   598  > **Note**:
   599  > In the *JSON* form, it is necessary to escape backslashes. This is
   600  > particularly relevant on Windows where the backslash is the path separator.
   601  > The following line would otherwise be treated as *shell* form due to not
   602  > being valid JSON, and fail in an unexpected way:
   603  > `RUN ["c:\windows\system32\tasklist.exe"]`
   604  > The correct syntax for this example is:
   605  > `RUN ["c:\\windows\\system32\\tasklist.exe"]`
   606  
   607  The cache for `RUN` instructions isn't invalidated automatically during
   608  the next build. The cache for an instruction like
   609  `RUN apt-get dist-upgrade -y` will be reused during the next build. The
   610  cache for `RUN` instructions can be invalidated by using the `--no-cache`
   611  flag, for example `docker build --no-cache`.
   612  
   613  See the [`Dockerfile` Best Practices
   614  guide](https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#/build-cache) for more information.
   615  
   616  The cache for `RUN` instructions can be invalidated by `ADD` instructions. See
   617  [below](#add) for details.
   618  
   619  ### Known issues (RUN)
   620  
   621  - [Issue 783](https://github.com/docker/docker/issues/783) is about file
   622    permissions problems that can occur when using the AUFS file system. You
   623    might notice it during an attempt to `rm` a file, for example.
   624  
   625    For systems that have recent aufs version (i.e., `dirperm1` mount option can
   626    be set), docker will attempt to fix the issue automatically by mounting
   627    the layers with `dirperm1` option. More details on `dirperm1` option can be
   628    found at [`aufs` man page](https://github.com/sfjro/aufs3-linux/tree/aufs3.18/Documentation/filesystems/aufs)
   629  
   630    If your system doesn't have support for `dirperm1`, the issue describes a workaround.
   631  
   632  ## CMD
   633  
   634  The `CMD` instruction has three forms:
   635  
   636  - `CMD ["executable","param1","param2"]` (*exec* form, this is the preferred form)
   637  - `CMD ["param1","param2"]` (as *default parameters to ENTRYPOINT*)
   638  - `CMD command param1 param2` (*shell* form)
   639  
   640  There can only be one `CMD` instruction in a `Dockerfile`. If you list more than one `CMD`
   641  then only the last `CMD` will take effect.
   642  
   643  **The main purpose of a `CMD` is to provide defaults for an executing
   644  container.** These defaults can include an executable, or they can omit
   645  the executable, in which case you must specify an `ENTRYPOINT`
   646  instruction as well.
   647  
   648  > **Note**:
   649  > If `CMD` is used to provide default arguments for the `ENTRYPOINT`
   650  > instruction, both the `CMD` and `ENTRYPOINT` instructions should be specified
   651  > with the JSON array format.
   652  
   653  > **Note**:
   654  > The *exec* form is parsed as a JSON array, which means that
   655  > you must use double-quotes (") around words not single-quotes (').
   656  
   657  > **Note**:
   658  > Unlike the *shell* form, the *exec* form does not invoke a command shell.
   659  > This means that normal shell processing does not happen. For example,
   660  > `CMD [ "echo", "$HOME" ]` will not do variable substitution on `$HOME`.
   661  > If you want shell processing then either use the *shell* form or execute
   662  > a shell directly, for example: `CMD [ "sh", "-c", "echo $HOME" ]`.
   663  > When using the exec form and executing a shell directly, as in the case for
   664  > the shell form, it is the shell that is doing the environment variable
   665  > expansion, not docker.
   666  
   667  When used in the shell or exec formats, the `CMD` instruction sets the command
   668  to be executed when running the image.
   669  
   670  If you use the *shell* form of the `CMD`, then the `<command>` will execute in
   671  `/bin/sh -c`:
   672  
   673      FROM ubuntu
   674      CMD echo "This is a test." | wc -
   675  
   676  If you want to **run your** `<command>` **without a shell** then you must
   677  express the command as a JSON array and give the full path to the executable.
   678  **This array form is the preferred format of `CMD`.** Any additional parameters
   679  must be individually expressed as strings in the array:
   680  
   681      FROM ubuntu
   682      CMD ["/usr/bin/wc","--help"]
   683  
   684  If you would like your container to run the same executable every time, then
   685  you should consider using `ENTRYPOINT` in combination with `CMD`. See
   686  [*ENTRYPOINT*](#entrypoint).
   687  
   688  If the user specifies arguments to `docker run` then they will override the
   689  default specified in `CMD`.
   690  
   691  > **Note**:
   692  > Don't confuse `RUN` with `CMD`. `RUN` actually runs a command and commits
   693  > the result; `CMD` does not execute anything at build time, but specifies
   694  > the intended command for the image.
   695  
   696  ## LABEL
   697  
   698      LABEL <key>=<value> <key>=<value> <key>=<value> ...
   699  
   700  The `LABEL` instruction adds metadata to an image. A `LABEL` is a
   701  key-value pair. To include spaces within a `LABEL` value, use quotes and
   702  backslashes as you would in command-line parsing. A few usage examples:
   703  
   704      LABEL "com.example.vendor"="ACME Incorporated"
   705      LABEL com.example.label-with-value="foo"
   706      LABEL version="1.0"
   707      LABEL description="This text illustrates \
   708      that label-values can span multiple lines."
   709  
   710  An image can have more than one label. To specify multiple labels,
   711  Docker recommends combining labels into a single `LABEL` instruction where
   712  possible. Each `LABEL` instruction produces a new layer which can result in an
   713  inefficient image if you use many labels. This example results in a single image
   714  layer.
   715  
   716      LABEL multi.label1="value1" multi.label2="value2" other="value3"
   717  
   718  The above can also be written as:
   719  
   720      LABEL multi.label1="value1" \
   721            multi.label2="value2" \
   722            other="value3"
   723  
   724  Labels are additive including `LABEL`s in `FROM` images. If Docker
   725  encounters a label/key that already exists, the new value overrides any previous
   726  labels with identical keys.
   727  
   728  To view an image's labels, use the `docker inspect` command.
   729  
   730      "Labels": {
   731          "com.example.vendor": "ACME Incorporated"
   732          "com.example.label-with-value": "foo",
   733          "version": "1.0",
   734          "description": "This text illustrates that label-values can span multiple lines.",
   735          "multi.label1": "value1",
   736          "multi.label2": "value2",
   737          "other": "value3"
   738      },
   739  
   740  ## MAINTAINER (deprecated)
   741  
   742      MAINTAINER <name>
   743  
   744  The `MAINTAINER` instruction sets the *Author* field of the generated images.
   745  The `LABEL` instruction is a much more flexible version of this and you should use
   746  it instead, as it enables setting any metadata you require, and can be viewed
   747  easily, for example with `docker inspect`. To set a label corresponding to the
   748  `MAINTAINER` field you could use:
   749  
   750      LABEL maintainer="SvenDowideit@home.org.au"
   751  
   752  This will then be visible from `docker inspect` with the other labels.
   753  
   754  ## EXPOSE
   755  
   756      EXPOSE <port> [<port>...]
   757  
   758  The `EXPOSE` instruction informs Docker that the container listens on the
   759  specified network ports at runtime. `EXPOSE` does not make the ports of the
   760  container accessible to the host. To do that, you must use either the `-p` flag
   761  to publish a range of ports or the `-P` flag to publish all of the exposed
   762  ports. You can expose one port number and publish it externally under another
   763  number.
   764  
   765  To set up port redirection on the host system, see [using the -P
   766  flag](run.md#expose-incoming-ports). The Docker network feature supports
   767  creating networks without the need to expose ports within the network, for
   768  detailed information see the  [overview of this
   769  feature](https://docs.docker.com/engine/userguide/networking/)).
   770  
   771  ## ENV
   772  
   773      ENV <key> <value>
   774      ENV <key>=<value> ...
   775  
   776  The `ENV` instruction sets the environment variable `<key>` to the value
   777  `<value>`. This value will be in the environment of all "descendant"
   778  `Dockerfile` commands and can be [replaced inline](#environment-replacement) in
   779  many as well.
   780  
   781  The `ENV` instruction has two forms. The first form, `ENV <key> <value>`,
   782  will set a single variable to a value. The entire string after the first
   783  space will be treated as the `<value>` - including characters such as
   784  spaces and quotes.
   785  
   786  The second form, `ENV <key>=<value> ...`, allows for multiple variables to
   787  be set at one time. Notice that the second form uses the equals sign (=)
   788  in the syntax, while the first form does not. Like command line parsing,
   789  quotes and backslashes can be used to include spaces within values.
   790  
   791  For example:
   792  
   793      ENV myName="John Doe" myDog=Rex\ The\ Dog \
   794          myCat=fluffy
   795  
   796  and
   797  
   798      ENV myName John Doe
   799      ENV myDog Rex The Dog
   800      ENV myCat fluffy
   801  
   802  will yield the same net results in the final image, but the first form
   803  is preferred because it produces a single cache layer.
   804  
   805  The environment variables set using `ENV` will persist when a container is run
   806  from the resulting image. You can view the values using `docker inspect`, and
   807  change them using `docker run --env <key>=<value>`.
   808  
   809  > **Note**:
   810  > Environment persistence can cause unexpected side effects. For example,
   811  > setting `ENV DEBIAN_FRONTEND noninteractive` may confuse apt-get
   812  > users on a Debian-based image. To set a value for a single command, use
   813  > `RUN <key>=<value> <command>`.
   814  
   815  ## ADD
   816  
   817  ADD has two forms:
   818  
   819  - `ADD <src>... <dest>`
   820  - `ADD ["<src>",... "<dest>"]` (this form is required for paths containing
   821  whitespace)
   822  
   823  The `ADD` instruction copies new files, directories or remote file URLs from `<src>`
   824  and adds them to the filesystem of the image at the path `<dest>`.
   825  
   826  Multiple `<src>` resource may be specified but if they are files or
   827  directories then they must be relative to the source directory that is
   828  being built (the context of the build).
   829  
   830  Each `<src>` may contain wildcards and matching will be done using Go's
   831  [filepath.Match](http://golang.org/pkg/path/filepath#Match) rules. For example:
   832  
   833      ADD hom* /mydir/        # adds all files starting with "hom"
   834      ADD hom?.txt /mydir/    # ? is replaced with any single character, e.g., "home.txt"
   835  
   836  The `<dest>` is an absolute path, or a path relative to `WORKDIR`, into which
   837  the source will be copied inside the destination container.
   838  
   839      ADD test relativeDir/          # adds "test" to `WORKDIR`/relativeDir/
   840      ADD test /absoluteDir/         # adds "test" to /absoluteDir/
   841  
   842  When adding files or directories that contain special characters (such as `[`
   843  and `]`), you need to escape those paths following the Golang rules to prevent
   844  them from being treated as a matching pattern. For example, to add a file
   845  named `arr[0].txt`, use the following;
   846  
   847      ADD arr[[]0].txt /mydir/    # copy a file named "arr[0].txt" to /mydir/
   848  
   849  
   850  All new files and directories are created with a UID and GID of 0.
   851  
   852  In the case where `<src>` is a remote file URL, the destination will
   853  have permissions of 600. If the remote file being retrieved has an HTTP
   854  `Last-Modified` header, the timestamp from that header will be used
   855  to set the `mtime` on the destination file. However, like any other file
   856  processed during an `ADD`, `mtime` will not be included in the determination
   857  of whether or not the file has changed and the cache should be updated.
   858  
   859  > **Note**:
   860  > If you build by passing a `Dockerfile` through STDIN (`docker
   861  > build - < somefile`), there is no build context, so the `Dockerfile`
   862  > can only contain a URL based `ADD` instruction. You can also pass a
   863  > compressed archive through STDIN: (`docker build - < archive.tar.gz`),
   864  > the `Dockerfile` at the root of the archive and the rest of the
   865  > archive will be used as the context of the build.
   866  
   867  > **Note**:
   868  > If your URL files are protected using authentication, you
   869  > will need to use `RUN wget`, `RUN curl` or use another tool from
   870  > within the container as the `ADD` instruction does not support
   871  > authentication.
   872  
   873  > **Note**:
   874  > The first encountered `ADD` instruction will invalidate the cache for all
   875  > following instructions from the Dockerfile if the contents of `<src>` have
   876  > changed. This includes invalidating the cache for `RUN` instructions.
   877  > See the [`Dockerfile` Best Practices
   878  guide](https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#/build-cache) for more information.
   879  
   880  
   881  `ADD` obeys the following rules:
   882  
   883  - The `<src>` path must be inside the *context* of the build;
   884    you cannot `ADD ../something /something`, because the first step of a
   885    `docker build` is to send the context directory (and subdirectories) to the
   886    docker daemon.
   887  
   888  - If `<src>` is a URL and `<dest>` does not end with a trailing slash, then a
   889    file is downloaded from the URL and copied to `<dest>`.
   890  
   891  - If `<src>` is a URL and `<dest>` does end with a trailing slash, then the
   892    filename is inferred from the URL and the file is downloaded to
   893    `<dest>/<filename>`. For instance, `ADD http://example.com/foobar /` would
   894    create the file `/foobar`. The URL must have a nontrivial path so that an
   895    appropriate filename can be discovered in this case (`http://example.com`
   896    will not work).
   897  
   898  - If `<src>` is a directory, the entire contents of the directory are copied,
   899    including filesystem metadata.
   900  
   901  > **Note**:
   902  > The directory itself is not copied, just its contents.
   903  
   904  - If `<src>` is a *local* tar archive in a recognized compression format
   905    (identity, gzip, bzip2 or xz) then it is unpacked as a directory. Resources
   906    from *remote* URLs are **not** decompressed. When a directory is copied or
   907    unpacked, it has the same behavior as `tar -x`, the result is the union of:
   908  
   909      1. Whatever existed at the destination path and
   910      2. The contents of the source tree, with conflicts resolved in favor
   911         of "2." on a file-by-file basis.
   912  
   913    > **Note**:
   914    > Whether a file is identified as a recognized compression format or not
   915    > is done solely based on the contents of the file, not the name of the file.
   916    > For example, if an empty file happens to end with `.tar.gz` this will not
   917    > be recognized as a compressed file and **will not** generate any kind of
   918    > decompression error message, rather the file will simply be copied to the
   919    > destination.
   920  
   921  - If `<src>` is any other kind of file, it is copied individually along with
   922    its metadata. In this case, if `<dest>` ends with a trailing slash `/`, it
   923    will be considered a directory and the contents of `<src>` will be written
   924    at `<dest>/base(<src>)`.
   925  
   926  - If multiple `<src>` resources are specified, either directly or due to the
   927    use of a wildcard, then `<dest>` must be a directory, and it must end with
   928    a slash `/`.
   929  
   930  - If `<dest>` does not end with a trailing slash, it will be considered a
   931    regular file and the contents of `<src>` will be written at `<dest>`.
   932  
   933  - If `<dest>` doesn't exist, it is created along with all missing directories
   934    in its path.
   935  
   936  ## COPY
   937  
   938  COPY has two forms:
   939  
   940  - `COPY <src>... <dest>`
   941  - `COPY ["<src>",... "<dest>"]` (this form is required for paths containing
   942  whitespace)
   943  
   944  The `COPY` instruction copies new files or directories from `<src>`
   945  and adds them to the filesystem of the container at the path `<dest>`.
   946  
   947  Multiple `<src>` resource may be specified but they must be relative
   948  to the source directory that is being built (the context of the build).
   949  
   950  Each `<src>` may contain wildcards and matching will be done using Go's
   951  [filepath.Match](http://golang.org/pkg/path/filepath#Match) rules. For example:
   952  
   953      COPY hom* /mydir/        # adds all files starting with "hom"
   954      COPY hom?.txt /mydir/    # ? is replaced with any single character, e.g., "home.txt"
   955  
   956  The `<dest>` is an absolute path, or a path relative to `WORKDIR`, into which
   957  the source will be copied inside the destination container.
   958  
   959      COPY test relativeDir/   # adds "test" to `WORKDIR`/relativeDir/
   960      COPY test /absoluteDir/  # adds "test" to /absoluteDir/
   961  
   962  
   963  When copying files or directories that contain special characters (such as `[`
   964  and `]`), you need to escape those paths following the Golang rules to prevent
   965  them from being treated as a matching pattern. For example, to copy a file
   966  named `arr[0].txt`, use the following;
   967  
   968      COPY arr[[]0].txt /mydir/    # copy a file named "arr[0].txt" to /mydir/
   969  
   970  All new files and directories are created with a UID and GID of 0.
   971  
   972  > **Note**:
   973  > If you build using STDIN (`docker build - < somefile`), there is no
   974  > build context, so `COPY` can't be used.
   975  
   976  Optionally `COPY` accepts a flag `--from=<name|index>` that can be used to set
   977  the source location to a previous build stage (created with `FROM .. AS <name>`)
   978  that will be used instead of a build context sent by the user. The flag also 
   979  accepts a numeric index assigned for all previous build stages started with 
   980  `FROM` instruction. In case a build stage with a specified name can't be found an 
   981  image with the same name is attempted to be used instead.
   982  
   983  `COPY` obeys the following rules:
   984  
   985  - The `<src>` path must be inside the *context* of the build;
   986    you cannot `COPY ../something /something`, because the first step of a
   987    `docker build` is to send the context directory (and subdirectories) to the
   988    docker daemon.
   989  
   990  - If `<src>` is a directory, the entire contents of the directory are copied,
   991    including filesystem metadata.
   992  
   993  > **Note**:
   994  > The directory itself is not copied, just its contents.
   995  
   996  - If `<src>` is any other kind of file, it is copied individually along with
   997    its metadata. In this case, if `<dest>` ends with a trailing slash `/`, it
   998    will be considered a directory and the contents of `<src>` will be written
   999    at `<dest>/base(<src>)`.
  1000  
  1001  - If multiple `<src>` resources are specified, either directly or due to the
  1002    use of a wildcard, then `<dest>` must be a directory, and it must end with
  1003    a slash `/`.
  1004  
  1005  - If `<dest>` does not end with a trailing slash, it will be considered a
  1006    regular file and the contents of `<src>` will be written at `<dest>`.
  1007  
  1008  - If `<dest>` doesn't exist, it is created along with all missing directories
  1009    in its path.
  1010  
  1011  ## ENTRYPOINT
  1012  
  1013  ENTRYPOINT has two forms:
  1014  
  1015  - `ENTRYPOINT ["executable", "param1", "param2"]`
  1016    (*exec* form, preferred)
  1017  - `ENTRYPOINT command param1 param2`
  1018    (*shell* form)
  1019  
  1020  An `ENTRYPOINT` allows you to configure a container that will run as an executable.
  1021  
  1022  For example, the following will start nginx with its default content, listening
  1023  on port 80:
  1024  
  1025      docker run -i -t --rm -p 80:80 nginx
  1026  
  1027  Command line arguments to `docker run <image>` will be appended after all
  1028  elements in an *exec* form `ENTRYPOINT`, and will override all elements specified
  1029  using `CMD`.
  1030  This allows arguments to be passed to the entry point, i.e., `docker run <image> -d`
  1031  will pass the `-d` argument to the entry point.
  1032  You can override the `ENTRYPOINT` instruction using the `docker run --entrypoint`
  1033  flag.
  1034  
  1035  The *shell* form prevents any `CMD` or `run` command line arguments from being
  1036  used, but has the disadvantage that your `ENTRYPOINT` will be started as a
  1037  subcommand of `/bin/sh -c`, which does not pass signals.
  1038  This means that the executable will not be the container's `PID 1` - and
  1039  will _not_ receive Unix signals - so your executable will not receive a
  1040  `SIGTERM` from `docker stop <container>`.
  1041  
  1042  Only the last `ENTRYPOINT` instruction in the `Dockerfile` will have an effect.
  1043  
  1044  ### Exec form ENTRYPOINT example
  1045  
  1046  You can use the *exec* form of `ENTRYPOINT` to set fairly stable default commands
  1047  and arguments and then use either form of `CMD` to set additional defaults that
  1048  are more likely to be changed.
  1049  
  1050      FROM ubuntu
  1051      ENTRYPOINT ["top", "-b"]
  1052      CMD ["-c"]
  1053  
  1054  When you run the container, you can see that `top` is the only process:
  1055  
  1056      $ docker run -it --rm --name test  top -H
  1057      top - 08:25:00 up  7:27,  0 users,  load average: 0.00, 0.01, 0.05
  1058      Threads:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
  1059      %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
  1060      KiB Mem:   2056668 total,  1616832 used,   439836 free,    99352 buffers
  1061      KiB Swap:  1441840 total,        0 used,  1441840 free.  1324440 cached Mem
  1062  
  1063        PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
  1064          1 root      20   0   19744   2336   2080 R  0.0  0.1   0:00.04 top
  1065  
  1066  To examine the result further, you can use `docker exec`:
  1067  
  1068      $ docker exec -it test ps aux
  1069      USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
  1070      root         1  2.6  0.1  19752  2352 ?        Ss+  08:24   0:00 top -b -H
  1071      root         7  0.0  0.1  15572  2164 ?        R+   08:25   0:00 ps aux
  1072  
  1073  And you can gracefully request `top` to shut down using `docker stop test`.
  1074  
  1075  The following `Dockerfile` shows using the `ENTRYPOINT` to run Apache in the
  1076  foreground (i.e., as `PID 1`):
  1077  
  1078  ```
  1079  FROM debian:stable
  1080  RUN apt-get update && apt-get install -y --force-yes apache2
  1081  EXPOSE 80 443
  1082  VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
  1083  ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
  1084  ```
  1085  
  1086  If you need to write a starter script for a single executable, you can ensure that
  1087  the final executable receives the Unix signals by using `exec` and `gosu`
  1088  commands:
  1089  
  1090  ```bash
  1091  #!/usr/bin/env bash
  1092  set -e
  1093  
  1094  if [ "$1" = 'postgres' ]; then
  1095      chown -R postgres "$PGDATA"
  1096  
  1097      if [ -z "$(ls -A "$PGDATA")" ]; then
  1098          gosu postgres initdb
  1099      fi
  1100  
  1101      exec gosu postgres "$@"
  1102  fi
  1103  
  1104  exec "$@"
  1105  ```
  1106  
  1107  Lastly, if you need to do some extra cleanup (or communicate with other containers)
  1108  on shutdown, or are co-ordinating more than one executable, you may need to ensure
  1109  that the `ENTRYPOINT` script receives the Unix signals, passes them on, and then
  1110  does some more work:
  1111  
  1112  ```
  1113  #!/bin/sh
  1114  # Note: I've written this using sh so it works in the busybox container too
  1115  
  1116  # USE the trap if you need to also do manual cleanup after the service is stopped,
  1117  #     or need to start multiple services in the one container
  1118  trap "echo TRAPed signal" HUP INT QUIT TERM
  1119  
  1120  # start service in background here
  1121  /usr/sbin/apachectl start
  1122  
  1123  echo "[hit enter key to exit] or run 'docker stop <container>'"
  1124  read
  1125  
  1126  # stop service and clean up here
  1127  echo "stopping apache"
  1128  /usr/sbin/apachectl stop
  1129  
  1130  echo "exited $0"
  1131  ```
  1132  
  1133  If you run this image with `docker run -it --rm -p 80:80 --name test apache`,
  1134  you can then examine the container's processes with `docker exec`, or `docker top`,
  1135  and then ask the script to stop Apache:
  1136  
  1137  ```bash
  1138  $ docker exec -it test ps aux
  1139  USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
  1140  root         1  0.1  0.0   4448   692 ?        Ss+  00:42   0:00 /bin/sh /run.sh 123 cmd cmd2
  1141  root        19  0.0  0.2  71304  4440 ?        Ss   00:42   0:00 /usr/sbin/apache2 -k start
  1142  www-data    20  0.2  0.2 360468  6004 ?        Sl   00:42   0:00 /usr/sbin/apache2 -k start
  1143  www-data    21  0.2  0.2 360468  6000 ?        Sl   00:42   0:00 /usr/sbin/apache2 -k start
  1144  root        81  0.0  0.1  15572  2140 ?        R+   00:44   0:00 ps aux
  1145  $ docker top test
  1146  PID                 USER                COMMAND
  1147  10035               root                {run.sh} /bin/sh /run.sh 123 cmd cmd2
  1148  10054               root                /usr/sbin/apache2 -k start
  1149  10055               33                  /usr/sbin/apache2 -k start
  1150  10056               33                  /usr/sbin/apache2 -k start
  1151  $ /usr/bin/time docker stop test
  1152  test
  1153  real	0m 0.27s
  1154  user	0m 0.03s
  1155  sys	0m 0.03s
  1156  ```
  1157  
  1158  > **Note:** you can override the `ENTRYPOINT` setting using `--entrypoint`,
  1159  > but this can only set the binary to *exec* (no `sh -c` will be used).
  1160  
  1161  > **Note**:
  1162  > The *exec* form is parsed as a JSON array, which means that
  1163  > you must use double-quotes (") around words not single-quotes (').
  1164  
  1165  > **Note**:
  1166  > Unlike the *shell* form, the *exec* form does not invoke a command shell.
  1167  > This means that normal shell processing does not happen. For example,
  1168  > `ENTRYPOINT [ "echo", "$HOME" ]` will not do variable substitution on `$HOME`.
  1169  > If you want shell processing then either use the *shell* form or execute
  1170  > a shell directly, for example: `ENTRYPOINT [ "sh", "-c", "echo $HOME" ]`.
  1171  > When using the exec form and executing a shell directly, as in the case for
  1172  > the shell form, it is the shell that is doing the environment variable
  1173  > expansion, not docker.
  1174  
  1175  ### Shell form ENTRYPOINT example
  1176  
  1177  You can specify a plain string for the `ENTRYPOINT` and it will execute in `/bin/sh -c`.
  1178  This form will use shell processing to substitute shell environment variables,
  1179  and will ignore any `CMD` or `docker run` command line arguments.
  1180  To ensure that `docker stop` will signal any long running `ENTRYPOINT` executable
  1181  correctly, you need to remember to start it with `exec`:
  1182  
  1183      FROM ubuntu
  1184      ENTRYPOINT exec top -b
  1185  
  1186  When you run this image, you'll see the single `PID 1` process:
  1187  
  1188      $ docker run -it --rm --name test top
  1189      Mem: 1704520K used, 352148K free, 0K shrd, 0K buff, 140368121167873K cached
  1190      CPU:   5% usr   0% sys   0% nic  94% idle   0% io   0% irq   0% sirq
  1191      Load average: 0.08 0.03 0.05 2/98 6
  1192        PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
  1193          1     0 root     R     3164   0%   0% top -b
  1194  
  1195  Which will exit cleanly on `docker stop`:
  1196  
  1197      $ /usr/bin/time docker stop test
  1198      test
  1199      real	0m 0.20s
  1200      user	0m 0.02s
  1201      sys	0m 0.04s
  1202  
  1203  If you forget to add `exec` to the beginning of your `ENTRYPOINT`:
  1204  
  1205      FROM ubuntu
  1206      ENTRYPOINT top -b
  1207      CMD --ignored-param1
  1208  
  1209  You can then run it (giving it a name for the next step):
  1210  
  1211      $ docker run -it --name test top --ignored-param2
  1212      Mem: 1704184K used, 352484K free, 0K shrd, 0K buff, 140621524238337K cached
  1213      CPU:   9% usr   2% sys   0% nic  88% idle   0% io   0% irq   0% sirq
  1214      Load average: 0.01 0.02 0.05 2/101 7
  1215        PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
  1216          1     0 root     S     3168   0%   0% /bin/sh -c top -b cmd cmd2
  1217          7     1 root     R     3164   0%   0% top -b
  1218  
  1219  You can see from the output of `top` that the specified `ENTRYPOINT` is not `PID 1`.
  1220  
  1221  If you then run `docker stop test`, the container will not exit cleanly - the
  1222  `stop` command will be forced to send a `SIGKILL` after the timeout:
  1223  
  1224      $ docker exec -it test ps aux
  1225      PID   USER     COMMAND
  1226          1 root     /bin/sh -c top -b cmd cmd2
  1227          7 root     top -b
  1228          8 root     ps aux
  1229      $ /usr/bin/time docker stop test
  1230      test
  1231      real	0m 10.19s
  1232      user	0m 0.04s
  1233      sys	0m 0.03s
  1234  
  1235  ### Understand how CMD and ENTRYPOINT interact
  1236  
  1237  Both `CMD` and `ENTRYPOINT` instructions define what command gets executed when running a container.
  1238  There are few rules that describe their co-operation.
  1239  
  1240  1. Dockerfile should specify at least one of `CMD` or `ENTRYPOINT` commands.
  1241  
  1242  2. `ENTRYPOINT` should be defined when using the container as an executable.
  1243  
  1244  3. `CMD` should be used as a way of defining default arguments for an `ENTRYPOINT` command
  1245  or for executing an ad-hoc command in a container.
  1246  
  1247  4. `CMD` will be overridden when running the container with alternative arguments.
  1248  
  1249  The table below shows what command is executed for different `ENTRYPOINT` / `CMD` combinations:
  1250  
  1251  |                                | No ENTRYPOINT              | ENTRYPOINT exec_entry p1_entry | ENTRYPOINT ["exec_entry", "p1_entry"]          |
  1252  |--------------------------------|----------------------------|--------------------------------|------------------------------------------------|
  1253  | **No CMD**                     | *error, not allowed*       | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry                            |
  1254  | **CMD ["exec_cmd", "p1_cmd"]** | exec_cmd p1_cmd            | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry exec_cmd p1_cmd            |
  1255  | **CMD ["p1_cmd", "p2_cmd"]**   | p1_cmd p2_cmd              | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry p1_cmd p2_cmd              |
  1256  | **CMD exec_cmd p1_cmd**        | /bin/sh -c exec_cmd p1_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd |
  1257  
  1258  ## VOLUME
  1259  
  1260      VOLUME ["/data"]
  1261  
  1262  The `VOLUME` instruction creates a mount point with the specified name
  1263  and marks it as holding externally mounted volumes from native host or other
  1264  containers. The value can be a JSON array, `VOLUME ["/var/log/"]`, or a plain
  1265  string with multiple arguments, such as `VOLUME /var/log` or `VOLUME /var/log
  1266  /var/db`. For more information/examples and mounting instructions via the
  1267  Docker client, refer to
  1268  [*Share Directories via Volumes*](https://docs.docker.com/engine/tutorials/dockervolumes/#/mount-a-host-directory-as-a-data-volume)
  1269  documentation.
  1270  
  1271  The `docker run` command initializes the newly created volume with any data
  1272  that exists at the specified location within the base image. For example,
  1273  consider the following Dockerfile snippet:
  1274  
  1275      FROM ubuntu
  1276      RUN mkdir /myvol
  1277      RUN echo "hello world" > /myvol/greeting
  1278      VOLUME /myvol
  1279  
  1280  This Dockerfile results in an image that causes `docker run`, to
  1281  create a new mount point at `/myvol` and copy the  `greeting` file
  1282  into the newly created volume.
  1283  
  1284  > **Note**:
  1285  > When using Windows-based containers, the destination of a volume inside the
  1286  > container must be one of: a non-existing or empty directory; or a drive other
  1287  > than C:.
  1288  
  1289  > **Note**:
  1290  > If any build steps change the data within the volume after it has been
  1291  > declared, those changes will be discarded.
  1292  
  1293  > **Note**:
  1294  > The list is parsed as a JSON array, which means that
  1295  > you must use double-quotes (") around words not single-quotes (').
  1296  
  1297  ## USER
  1298  
  1299      USER daemon
  1300  
  1301  The `USER` instruction sets the user name or UID to use when running the image
  1302  and for any `RUN`, `CMD` and `ENTRYPOINT` instructions that follow it in the
  1303  `Dockerfile`.
  1304  
  1305  ## WORKDIR
  1306  
  1307      WORKDIR /path/to/workdir
  1308  
  1309  The `WORKDIR` instruction sets the working directory for any `RUN`, `CMD`,
  1310  `ENTRYPOINT`, `COPY` and `ADD` instructions that follow it in the `Dockerfile`.
  1311  If the `WORKDIR` doesn't exist, it will be created even if it's not used in any
  1312  subsequent `Dockerfile` instruction.
  1313  
  1314  It can be used multiple times in the one `Dockerfile`. If a relative path
  1315  is provided, it will be relative to the path of the previous `WORKDIR`
  1316  instruction. For example:
  1317  
  1318      WORKDIR /a
  1319      WORKDIR b
  1320      WORKDIR c
  1321      RUN pwd
  1322  
  1323  The output of the final `pwd` command in this `Dockerfile` would be
  1324  `/a/b/c`.
  1325  
  1326  The `WORKDIR` instruction can resolve environment variables previously set using
  1327  `ENV`. You can only use environment variables explicitly set in the `Dockerfile`.
  1328  For example:
  1329  
  1330      ENV DIRPATH /path
  1331      WORKDIR $DIRPATH/$DIRNAME
  1332      RUN pwd
  1333  
  1334  The output of the final `pwd` command in this `Dockerfile` would be
  1335  `/path/$DIRNAME`
  1336  
  1337  ## ARG
  1338  
  1339      ARG <name>[=<default value>]
  1340  
  1341  The `ARG` instruction defines a variable that users can pass at build-time to
  1342  the builder with the `docker build` command using the `--build-arg <varname>=<value>`
  1343  flag. If a user specifies a build argument that was not
  1344  defined in the Dockerfile, the build outputs a warning.
  1345  
  1346  ```
  1347  [Warning] One or more build-args [foo] were not consumed.
  1348  ```
  1349  
  1350  The Dockerfile author can define a single variable by specifying `ARG` once or many
  1351  variables by specifying `ARG` more than once. For example, a valid Dockerfile:
  1352  
  1353  ```
  1354  FROM busybox
  1355  ARG user1
  1356  ARG buildno
  1357  ...
  1358  ```
  1359  
  1360  A Dockerfile author may optionally specify a default value for an `ARG` instruction:
  1361  
  1362  ```
  1363  FROM busybox
  1364  ARG user1=someuser
  1365  ARG buildno=1
  1366  ...
  1367  ```
  1368  
  1369  If an `ARG` value has a default and if there is no value passed at build-time, the
  1370  builder uses the default.
  1371  
  1372  An `ARG` variable definition comes into effect from the line on which it is
  1373  defined in the `Dockerfile` not from the argument's use on the command-line or
  1374  elsewhere.  For example, consider this Dockerfile:
  1375  
  1376  ```
  1377  1 FROM busybox
  1378  2 USER ${user:-some_user}
  1379  3 ARG user
  1380  4 USER $user
  1381  ...
  1382  ```
  1383  A user builds this file by calling:
  1384  
  1385  ```
  1386  $ docker build --build-arg user=what_user .
  1387  ```
  1388  
  1389  The `USER` at line 2 evaluates to `some_user` as the `user` variable is defined on the
  1390  subsequent line 3. The `USER` at line 4 evaluates to `what_user` as `user` is
  1391  defined and the `what_user` value was passed on the command line. Prior to its definition by an
  1392  `ARG` instruction, any use of a variable results in an empty string.
  1393  
  1394  > **Warning:** It is not recommended to use build-time variables for
  1395  >  passing secrets like github keys, user credentials etc. Build-time variable
  1396  >  values are visible to any user of the image with the `docker history` command.
  1397  
  1398  You can use an `ARG` or an `ENV` instruction to specify variables that are
  1399  available to the `RUN` instruction. Environment variables defined using the
  1400  `ENV` instruction always override an `ARG` instruction of the same name. Consider
  1401  this Dockerfile with an `ENV` and `ARG` instruction.
  1402  
  1403  ```
  1404  1 FROM ubuntu
  1405  2 ARG CONT_IMG_VER
  1406  3 ENV CONT_IMG_VER v1.0.0
  1407  4 RUN echo $CONT_IMG_VER
  1408  ```
  1409  Then, assume this image is built with this command:
  1410  
  1411  ```
  1412  $ docker build --build-arg CONT_IMG_VER=v2.0.1 .
  1413  ```
  1414  
  1415  In this case, the `RUN` instruction uses `v1.0.0` instead of the `ARG` setting
  1416  passed by the user:`v2.0.1` This behavior is similar to a shell
  1417  script where a locally scoped variable overrides the variables passed as
  1418  arguments or inherited from environment, from its point of definition.
  1419  
  1420  Using the example above but a different `ENV` specification you can create more
  1421  useful interactions between `ARG` and `ENV` instructions:
  1422  
  1423  ```
  1424  1 FROM ubuntu
  1425  2 ARG CONT_IMG_VER
  1426  3 ENV CONT_IMG_VER ${CONT_IMG_VER:-v1.0.0}
  1427  4 RUN echo $CONT_IMG_VER
  1428  ```
  1429  
  1430  Unlike an `ARG` instruction, `ENV` values are always persisted in the built
  1431  image. Consider a docker build without the `--build-arg` flag:
  1432  
  1433  ```
  1434  $ docker build .
  1435  ```
  1436  
  1437  Using this Dockerfile example, `CONT_IMG_VER` is still persisted in the image but
  1438  its value would be `v1.0.0` as it is the default set in line 3 by the `ENV` instruction.
  1439  
  1440  The variable expansion technique in this example allows you to pass arguments
  1441  from the command line and persist them in the final image by leveraging the
  1442  `ENV` instruction. Variable expansion is only supported for [a limited set of
  1443  Dockerfile instructions.](#environment-replacement)
  1444  
  1445  Docker has a set of predefined `ARG` variables that you can use without a
  1446  corresponding `ARG` instruction in the Dockerfile.
  1447  
  1448  * `HTTP_PROXY`
  1449  * `http_proxy`
  1450  * `HTTPS_PROXY`
  1451  * `https_proxy`
  1452  * `FTP_PROXY`
  1453  * `ftp_proxy`
  1454  * `NO_PROXY`
  1455  * `no_proxy`
  1456  
  1457  To use these, simply pass them on the command line using the flag:
  1458  
  1459  ```
  1460  --build-arg <varname>=<value>
  1461  ```
  1462  
  1463  By default, these pre-defined variables are excluded from the output of
  1464  `docker history`. Excluding them reduces the risk of accidentally leaking
  1465  sensitive authentication information in an `HTTP_PROXY` variable.
  1466  
  1467  For example, consider building the following Dockerfile using
  1468  `--build-arg HTTP_PROXY=http://user:pass@proxy.lon.example.com`
  1469  
  1470  ``` Dockerfile
  1471  FROM ubuntu
  1472  RUN echo "Hello World"
  1473  ```
  1474  
  1475  In this case, the value of the `HTTP_PROXY` variable is not available in the
  1476  `docker history` and is not cached. If you were to change location, and your
  1477  proxy server changed to `http://user:pass@proxy.sfo.example.com`, a subsequent
  1478  build does not result in a cache miss.
  1479  
  1480  If you need to override this behaviour then you may do so by adding an `ARG`
  1481  statement in the Dockerfile as follows:
  1482  
  1483  ``` Dockerfile
  1484  FROM ubuntu
  1485  ARG HTTP_PROXY
  1486  RUN echo "Hello World"
  1487  ```
  1488  
  1489  When building this Dockerfile, the `HTTP_PROXY` is preserved in the
  1490  `docker history`, and changing its value invalidates the build cache.
  1491  
  1492  ### Impact on build caching
  1493  
  1494  `ARG` variables are not persisted into the built image as `ENV` variables are.
  1495  However, `ARG` variables do impact the build cache in similar ways. If a
  1496  Dockerfile defines an `ARG` variable whose value is different from a previous
  1497  build, then a "cache miss" occurs upon its first usage, not its definition. In
  1498  particular, all `RUN` instructions following an `ARG` instruction use the `ARG`
  1499  variable implicitly (as an environment variable), thus can cause a cache miss.
  1500  All predefined `ARG` variables are exempt from caching unless there is a
  1501  matching `ARG` statement in the `Dockerfile`.
  1502  
  1503  For example, consider these two Dockerfile:
  1504  
  1505  ```
  1506  1 FROM ubuntu
  1507  2 ARG CONT_IMG_VER
  1508  3 RUN echo $CONT_IMG_VER
  1509  ```
  1510  
  1511  ```
  1512  1 FROM ubuntu
  1513  2 ARG CONT_IMG_VER
  1514  3 RUN echo hello
  1515  ```
  1516  
  1517  If you specify `--build-arg CONT_IMG_VER=<value>` on the command line, in both
  1518  cases, the specification on line 2 does not cause a cache miss; line 3 does
  1519  cause a cache miss.`ARG CONT_IMG_VER` causes the RUN line to be identified
  1520  as the same as running `CONT_IMG_VER=<value>` echo hello, so if the `<value>`
  1521  changes, we get a cache miss.
  1522  
  1523  Consider another example under the same command line:
  1524  
  1525  ```
  1526  1 FROM ubuntu
  1527  2 ARG CONT_IMG_VER
  1528  3 ENV CONT_IMG_VER $CONT_IMG_VER
  1529  4 RUN echo $CONT_IMG_VER
  1530  ```
  1531  In this example, the cache miss occurs on line 3. The miss happens because
  1532  the variable's value in the `ENV` references the `ARG` variable and that
  1533  variable is changed through the command line. In this example, the `ENV`
  1534  command causes the image to include the value.
  1535  
  1536  If an `ENV` instruction overrides an `ARG` instruction of the same name, like
  1537  this Dockerfile:
  1538  
  1539  ```
  1540  1 FROM ubuntu
  1541  2 ARG CONT_IMG_VER
  1542  3 ENV CONT_IMG_VER hello
  1543  4 RUN echo $CONT_IMG_VER
  1544  ```
  1545  
  1546  Line 3 does not cause a cache miss because the value of `CONT_IMG_VER` is a
  1547  constant (`hello`). As a result, the environment variables and values used on
  1548  the `RUN` (line 4) doesn't change between builds.
  1549  
  1550  ## ONBUILD
  1551  
  1552      ONBUILD [INSTRUCTION]
  1553  
  1554  The `ONBUILD` instruction adds to the image a *trigger* instruction to
  1555  be executed at a later time, when the image is used as the base for
  1556  another build. The trigger will be executed in the context of the
  1557  downstream build, as if it had been inserted immediately after the
  1558  `FROM` instruction in the downstream `Dockerfile`.
  1559  
  1560  Any build instruction can be registered as a trigger.
  1561  
  1562  This is useful if you are building an image which will be used as a base
  1563  to build other images, for example an application build environment or a
  1564  daemon which may be customized with user-specific configuration.
  1565  
  1566  For example, if your image is a reusable Python application builder, it
  1567  will require application source code to be added in a particular
  1568  directory, and it might require a build script to be called *after*
  1569  that. You can't just call `ADD` and `RUN` now, because you don't yet
  1570  have access to the application source code, and it will be different for
  1571  each application build. You could simply provide application developers
  1572  with a boilerplate `Dockerfile` to copy-paste into their application, but
  1573  that is inefficient, error-prone and difficult to update because it
  1574  mixes with application-specific code.
  1575  
  1576  The solution is to use `ONBUILD` to register advance instructions to
  1577  run later, during the next build stage.
  1578  
  1579  Here's how it works:
  1580  
  1581  1. When it encounters an `ONBUILD` instruction, the builder adds a
  1582     trigger to the metadata of the image being built. The instruction
  1583     does not otherwise affect the current build.
  1584  2. At the end of the build, a list of all triggers is stored in the
  1585     image manifest, under the key `OnBuild`. They can be inspected with
  1586     the `docker inspect` command.
  1587  3. Later the image may be used as a base for a new build, using the
  1588     `FROM` instruction. As part of processing the `FROM` instruction,
  1589     the downstream builder looks for `ONBUILD` triggers, and executes
  1590     them in the same order they were registered. If any of the triggers
  1591     fail, the `FROM` instruction is aborted which in turn causes the
  1592     build to fail. If all triggers succeed, the `FROM` instruction
  1593     completes and the build continues as usual.
  1594  4. Triggers are cleared from the final image after being executed. In
  1595     other words they are not inherited by "grand-children" builds.
  1596  
  1597  For example you might add something like this:
  1598  
  1599      [...]
  1600      ONBUILD ADD . /app/src
  1601      ONBUILD RUN /usr/local/bin/python-build --dir /app/src
  1602      [...]
  1603  
  1604  > **Warning**: Chaining `ONBUILD` instructions using `ONBUILD ONBUILD` isn't allowed.
  1605  
  1606  > **Warning**: The `ONBUILD` instruction may not trigger `FROM` or `MAINTAINER` instructions.
  1607  
  1608  ## STOPSIGNAL
  1609  
  1610      STOPSIGNAL signal
  1611  
  1612  The `STOPSIGNAL` instruction sets the system call signal that will be sent to the container to exit.
  1613  This signal can be a valid unsigned number that matches a position in the kernel's syscall table, for instance 9,
  1614  or a signal name in the format SIGNAME, for instance SIGKILL.
  1615  
  1616  ## HEALTHCHECK
  1617  
  1618  The `HEALTHCHECK` instruction has two forms:
  1619  
  1620  * `HEALTHCHECK [OPTIONS] CMD command` (check container health by running a command inside the container)
  1621  * `HEALTHCHECK NONE` (disable any healthcheck inherited from the base image)
  1622  
  1623  The `HEALTHCHECK` instruction tells Docker how to test a container to check that
  1624  it is still working. This can detect cases such as a web server that is stuck in
  1625  an infinite loop and unable to handle new connections, even though the server
  1626  process is still running.
  1627  
  1628  When a container has a healthcheck specified, it has a _health status_ in
  1629  addition to its normal status. This status is initially `starting`. Whenever a
  1630  health check passes, it becomes `healthy` (whatever state it was previously in).
  1631  After a certain number of consecutive failures, it becomes `unhealthy`.
  1632  
  1633  The options that can appear before `CMD` are:
  1634  
  1635  * `--interval=DURATION` (default: `30s`)
  1636  * `--timeout=DURATION` (default: `30s`)
  1637  * `--start-period=DURATION` (default: `0s`)
  1638  * `--retries=N` (default: `3`)
  1639  
  1640  The health check will first run **interval** seconds after the container is
  1641  started, and then again **interval** seconds after each previous check completes.
  1642  
  1643  If a single run of the check takes longer than **timeout** seconds then the check
  1644  is considered to have failed.
  1645  
  1646  It takes **retries** consecutive failures of the health check for the container
  1647  to be considered `unhealthy`.
  1648  
  1649  **start period** provides initialization time for containers that need time to bootstrap.
  1650  Probe failure during that period will not be counted towards the maximum number of retries.
  1651  However, if a health check succeeds during the start period, the container is considered
  1652  started and all consecutive failures will be counted towards the maximum number of retries.
  1653  
  1654  There can only be one `HEALTHCHECK` instruction in a Dockerfile. If you list
  1655  more than one then only the last `HEALTHCHECK` will take effect.
  1656  
  1657  The command after the `CMD` keyword can be either a shell command (e.g. `HEALTHCHECK
  1658  CMD /bin/check-running`) or an _exec_ array (as with other Dockerfile commands;
  1659  see e.g. `ENTRYPOINT` for details).
  1660  
  1661  The command's exit status indicates the health status of the container.
  1662  The possible values are:
  1663  
  1664  - 0: success - the container is healthy and ready for use
  1665  - 1: unhealthy - the container is not working correctly
  1666  - 2: reserved - do not use this exit code
  1667  
  1668  For example, to check every five minutes or so that a web-server is able to
  1669  serve the site's main page within three seconds:
  1670  
  1671      HEALTHCHECK --interval=5m --timeout=3s \
  1672        CMD curl -f http://localhost/ || exit 1
  1673  
  1674  To help debug failing probes, any output text (UTF-8 encoded) that the command writes
  1675  on stdout or stderr will be stored in the health status and can be queried with
  1676  `docker inspect`. Such output should be kept short (only the first 4096 bytes
  1677  are stored currently).
  1678  
  1679  When the health status of a container changes, a `health_status` event is
  1680  generated with the new status.
  1681  
  1682  The `HEALTHCHECK` feature was added in Docker 1.12.
  1683  
  1684  
  1685  ## SHELL
  1686  
  1687      SHELL ["executable", "parameters"]
  1688  
  1689  The `SHELL` instruction allows the default shell used for the *shell* form of
  1690  commands to be overridden. The default shell on Linux is `["/bin/sh", "-c"]`, and on
  1691  Windows is `["cmd", "/S", "/C"]`. The `SHELL` instruction *must* be written in JSON
  1692  form in a Dockerfile.
  1693  
  1694  The `SHELL` instruction is particularly useful on Windows where there are
  1695  two commonly used and quite different native shells: `cmd` and `powershell`, as
  1696  well as alternate shells available including `sh`.
  1697  
  1698  The `SHELL` instruction can appear multiple times. Each `SHELL` instruction overrides
  1699  all previous `SHELL` instructions, and affects all subsequent instructions. For example:
  1700  
  1701      FROM microsoft/windowsservercore
  1702  
  1703      # Executed as cmd /S /C echo default
  1704      RUN echo default
  1705  
  1706      # Executed as cmd /S /C powershell -command Write-Host default
  1707      RUN powershell -command Write-Host default
  1708  
  1709      # Executed as powershell -command Write-Host hello
  1710      SHELL ["powershell", "-command"]
  1711      RUN Write-Host hello
  1712  
  1713      # Executed as cmd /S /C echo hello
  1714      SHELL ["cmd", "/S"", "/C"]
  1715      RUN echo hello
  1716  
  1717  The following instructions can be affected by the `SHELL` instruction when the
  1718  *shell* form of them is used in a Dockerfile: `RUN`, `CMD` and `ENTRYPOINT`.
  1719  
  1720  The following example is a common pattern found on Windows which can be
  1721  streamlined by using the `SHELL` instruction:
  1722  
  1723      ...
  1724      RUN powershell -command Execute-MyCmdlet -param1 "c:\foo.txt"
  1725      ...
  1726  
  1727  The command invoked by docker will be:
  1728  
  1729      cmd /S /C powershell -command Execute-MyCmdlet -param1 "c:\foo.txt"
  1730  
  1731  This is inefficient for two reasons. First, there is an un-necessary cmd.exe command
  1732  processor (aka shell) being invoked. Second, each `RUN` instruction in the *shell*
  1733  form requires an extra `powershell -command` prefixing the command.
  1734  
  1735  To make this more efficient, one of two mechanisms can be employed. One is to
  1736  use the JSON form of the RUN command such as:
  1737  
  1738      ...
  1739      RUN ["powershell", "-command", "Execute-MyCmdlet", "-param1 \"c:\\foo.txt\""]
  1740      ...
  1741  
  1742  While the JSON form is unambiguous and does not use the un-necessary cmd.exe,
  1743  it does require more verbosity through double-quoting and escaping. The alternate
  1744  mechanism is to use the `SHELL` instruction and the *shell* form,
  1745  making a more natural syntax for Windows users, especially when combined with
  1746  the `escape` parser directive:
  1747  
  1748      # escape=`
  1749  
  1750      FROM microsoft/nanoserver
  1751      SHELL ["powershell","-command"]
  1752      RUN New-Item -ItemType Directory C:\Example
  1753      ADD Execute-MyCmdlet.ps1 c:\example\
  1754      RUN c:\example\Execute-MyCmdlet -sample 'hello world'
  1755  
  1756  Resulting in:
  1757  
  1758      PS E:\docker\build\shell> docker build -t shell .
  1759      Sending build context to Docker daemon 4.096 kB
  1760      Step 1/5 : FROM microsoft/nanoserver
  1761       ---> 22738ff49c6d
  1762      Step 2/5 : SHELL powershell -command
  1763       ---> Running in 6fcdb6855ae2
  1764       ---> 6331462d4300
  1765      Removing intermediate container 6fcdb6855ae2
  1766      Step 3/5 : RUN New-Item -ItemType Directory C:\Example
  1767       ---> Running in d0eef8386e97
  1768      
  1769      
  1770          Directory: C:\
  1771      
  1772      
  1773      Mode                LastWriteTime         Length Name
  1774      ----                -------------         ------ ----
  1775      d-----       10/28/2016  11:26 AM                Example
  1776      
  1777      
  1778       ---> 3f2fbf1395d9
  1779      Removing intermediate container d0eef8386e97
  1780      Step 4/5 : ADD Execute-MyCmdlet.ps1 c:\example\
  1781       ---> a955b2621c31
  1782      Removing intermediate container b825593d39fc
  1783      Step 5/5 : RUN c:\example\Execute-MyCmdlet 'hello world'
  1784       ---> Running in be6d8e63fe75
  1785      hello world
  1786       ---> 8e559e9bf424
  1787      Removing intermediate container be6d8e63fe75
  1788      Successfully built 8e559e9bf424
  1789      PS E:\docker\build\shell>
  1790  
  1791  The `SHELL` instruction could also be used to modify the way in which
  1792  a shell operates. For example, using `SHELL cmd /S /C /V:ON|OFF` on Windows, delayed
  1793  environment variable expansion semantics could be modified.
  1794  
  1795  The `SHELL` instruction can also be used on Linux should an alternate shell be
  1796  required such as `zsh`, `csh`, `tcsh` and others.
  1797  
  1798  The `SHELL` feature was added in Docker 1.12.
  1799  
  1800  ## Dockerfile examples
  1801  
  1802  Below you can see some examples of Dockerfile syntax. If you're interested in
  1803  something more realistic, take a look at the list of [Dockerization examples](https://docs.docker.com/engine/examples/).
  1804  
  1805  ```
  1806  # Nginx
  1807  #
  1808  # VERSION               0.0.1
  1809  
  1810  FROM      ubuntu
  1811  LABEL Description="This image is used to start the foobar executable" Vendor="ACME Products" Version="1.0"
  1812  RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
  1813  ```
  1814  
  1815  ```
  1816  # Firefox over VNC
  1817  #
  1818  # VERSION               0.3
  1819  
  1820  FROM ubuntu
  1821  
  1822  # Install vnc, xvfb in order to create a 'fake' display and firefox
  1823  RUN apt-get update && apt-get install -y x11vnc xvfb firefox
  1824  RUN mkdir ~/.vnc
  1825  # Setup a password
  1826  RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
  1827  # Autostart firefox (might not be the best way, but it does the trick)
  1828  RUN bash -c 'echo "firefox" >> /.bashrc'
  1829  
  1830  EXPOSE 5900
  1831  CMD    ["x11vnc", "-forever", "-usepw", "-create"]
  1832  ```
  1833  
  1834  ```
  1835  # Multiple images example
  1836  #
  1837  # VERSION               0.1
  1838  
  1839  FROM ubuntu
  1840  RUN echo foo > bar
  1841  # Will output something like ===> 907ad6c2736f
  1842  
  1843  FROM ubuntu
  1844  RUN echo moo > oink
  1845  # Will output something like ===> 695d7793cbe4
  1846  
  1847  # You'll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
  1848  # /oink.
  1849  ```