github.com/Shopify/docker@v1.13.1/man/Dockerfile.5.md (about)

     1  % DOCKERFILE(5) Docker User Manuals
     2  % Zac Dover
     3  % May 2014
     4  # NAME
     5  
     6  Dockerfile - automate the steps of creating a Docker image
     7  
     8  # INTRODUCTION
     9  
    10  The **Dockerfile** is a configuration file that automates the steps of creating
    11  a Docker image. It is similar to a Makefile. Docker reads instructions from the
    12  **Dockerfile** to automate the steps otherwise performed manually to create an
    13  image. To build an image, create a file called **Dockerfile**.
    14  
    15  The **Dockerfile** describes the steps taken to assemble the image. When the
    16  **Dockerfile** has been created, call the `docker build` command, using the
    17  path of directory that contains **Dockerfile** as the argument.
    18  
    19  # SYNOPSIS
    20  
    21  INSTRUCTION arguments
    22  
    23  For example:
    24  
    25    FROM image
    26  
    27  # DESCRIPTION
    28  
    29  A Dockerfile is a file that automates the steps of creating a Docker image.
    30  A Dockerfile is similar to a Makefile.
    31  
    32  # USAGE
    33  
    34    docker build .
    35  
    36    -- Runs the steps and commits them, building a final image.
    37    The path to the source repository defines where to find the context of the
    38    build. The build is run by the Docker daemon, not the CLI. The whole
    39    context must be transferred to the daemon. The Docker CLI reports
    40    `"Sending build context to Docker daemon"` when the context is sent to the
    41    daemon.
    42  
    43    ```
    44    docker build -t repository/tag .
    45    ```
    46  
    47    -- specifies a repository and tag at which to save the new image if the build
    48    succeeds. The Docker daemon runs the steps one-by-one, committing the result
    49    to a new image if necessary, before finally outputting the ID of the new
    50    image. The Docker daemon automatically cleans up the context it is given.
    51  
    52    Docker re-uses intermediate images whenever possible. This significantly
    53    accelerates the *docker build* process.
    54  
    55  # FORMAT
    56  
    57    `FROM image`
    58  
    59    `FROM image:tag`
    60  
    61    `FROM image@digest`
    62  
    63    -- The **FROM** instruction sets the base image for subsequent instructions. A
    64    valid Dockerfile must have **FROM** as its first instruction. The image can be any
    65    valid image. It is easy to start by pulling an image from the public
    66    repositories.
    67  
    68    -- **FROM** must be the first non-comment instruction in Dockerfile.
    69  
    70    -- **FROM** may appear multiple times within a single Dockerfile in order to create
    71    multiple images. Make a note of the last image ID output by the commit before
    72    each new **FROM** command.
    73  
    74    -- If no tag is given to the **FROM** instruction, Docker applies the
    75    `latest` tag. If the used tag does not exist, an error is returned.
    76  
    77    -- If no digest is given to the **FROM** instruction, Docker applies the
    78    `latest` tag. If the used tag does not exist, an error is returned.
    79  
    80  **MAINTAINER**
    81    -- **MAINTAINER** sets the Author field for the generated images.
    82    Useful for providing users with an email or url for support.
    83  
    84  **RUN**
    85    -- **RUN** has two forms:
    86  
    87    ```
    88    # the command is run in a shell - /bin/sh -c
    89    RUN <command>
    90  
    91    # Executable form
    92    RUN ["executable", "param1", "param2"]
    93    ```
    94  
    95  
    96    -- The **RUN** instruction executes any commands in a new layer on top of the current
    97    image and commits the results. The committed image is used for the next step in
    98    Dockerfile.
    99  
   100    -- Layering **RUN** instructions and generating commits conforms to the core
   101    concepts of Docker where commits are cheap and containers can be created from
   102    any point in the history of an image. This is similar to source control.  The
   103    exec form makes it possible to avoid shell string munging. The exec form makes
   104    it possible to **RUN** commands using a base image that does not contain `/bin/sh`.
   105  
   106    Note that the exec form is parsed as a JSON array, which means that you must
   107    use double-quotes (") around words not single-quotes (').
   108  
   109  **CMD**
   110    -- **CMD** has three forms:
   111  
   112    ```
   113    # Executable form
   114    CMD ["executable", "param1", "param2"]`
   115  
   116    # Provide default arguments to ENTRYPOINT
   117    CMD ["param1", "param2"]`
   118  
   119    # the command is run in a shell - /bin/sh -c
   120    CMD command param1 param2
   121    ```
   122  
   123    -- There should be only one **CMD** in a Dockerfile. If more than one **CMD** is listed, only
   124    the last **CMD** takes effect.
   125    The main purpose of a **CMD** is to provide defaults for an executing container.
   126    These defaults may include an executable, or they can omit the executable. If
   127    they omit the executable, an **ENTRYPOINT** must be specified.
   128    When used in the shell or exec formats, the **CMD** instruction sets the command to
   129    be executed when running the image.
   130    If you use the shell form of the **CMD**, the `<command>` executes in `/bin/sh -c`:
   131  
   132    Note that the exec form is parsed as a JSON array, which means that you must
   133    use double-quotes (") around words not single-quotes (').
   134  
   135    ```
   136    FROM ubuntu
   137    CMD echo "This is a test." | wc -
   138    ```
   139  
   140    -- If you run **command** without a shell, then you must express the command as a
   141    JSON array and give the full path to the executable. This array form is the
   142    preferred form of **CMD**. All additional parameters must be individually expressed
   143    as strings in the array:
   144  
   145    ```
   146    FROM ubuntu
   147    CMD ["/usr/bin/wc","--help"]
   148    ```
   149  
   150    -- To make the container run the same executable every time, use **ENTRYPOINT** in
   151    combination with **CMD**.
   152    If the user specifies arguments to `docker run`, the specified commands
   153    override the default in **CMD**.
   154    Do not confuse **RUN** with **CMD**. **RUN** runs a command and commits the result.
   155    **CMD** executes nothing at build time, but specifies the intended command for
   156    the image.
   157  
   158  **LABEL**
   159    -- `LABEL <key>=<value> [<key>=<value> ...]`or
   160    ```
   161    LABEL <key>[ <value>]
   162    LABEL <key>[ <value>]
   163    ...
   164    ```
   165    The **LABEL** instruction adds metadata to an image. A **LABEL** is a
   166    key-value pair. To specify a **LABEL** without a value, simply use an empty
   167    string. To include spaces within a **LABEL** value, use quotes and
   168    backslashes as you would in command-line parsing.
   169  
   170    ```
   171    LABEL com.example.vendor="ACME Incorporated"
   172    LABEL com.example.vendor "ACME Incorporated"
   173    LABEL com.example.vendor.is-beta ""
   174    LABEL com.example.vendor.is-beta=
   175    LABEL com.example.vendor.is-beta=""
   176    ```
   177  
   178    An image can have more than one label. To specify multiple labels, separate
   179    each key-value pair by a space.
   180  
   181    Labels are additive including `LABEL`s in `FROM` images. As the system
   182    encounters and then applies a new label, new `key`s override any previous
   183    labels with identical keys.
   184  
   185    To display an image's labels, use the `docker inspect` command.
   186  
   187  **EXPOSE**
   188    -- `EXPOSE <port> [<port>...]`
   189    The **EXPOSE** instruction informs Docker that the container listens on the
   190    specified network ports at runtime. Docker uses this information to
   191    interconnect containers using links and to set up port redirection on the host
   192    system.
   193  
   194  **ENV**
   195    -- `ENV <key> <value>`
   196    The **ENV** instruction sets the environment variable <key> to
   197    the value `<value>`. This value is passed to all future
   198    **RUN**, **ENTRYPOINT**, and **CMD** instructions. This is
   199    functionally equivalent to prefixing the command with `<key>=<value>`.  The
   200    environment variables that are set with **ENV** persist when a container is run
   201    from the resulting image. Use `docker inspect` to inspect these values, and
   202    change them using `docker run --env <key>=<value>`.
   203  
   204    Note that setting "`ENV DEBIAN_FRONTEND noninteractive`" may cause
   205    unintended consequences, because it will persist when the container is run
   206    interactively, as with the following command: `docker run -t -i image bash`
   207  
   208  **ADD**
   209    -- **ADD** has two forms:
   210  
   211    ```
   212    ADD <src> <dest>
   213  
   214    # Required for paths with whitespace
   215    ADD ["<src>",... "<dest>"]
   216    ```
   217  
   218    The **ADD** instruction copies new files, directories
   219    or remote file URLs to the filesystem of the container at path `<dest>`.
   220    Multiple `<src>` resources may be specified but if they are files or directories
   221    then they must be relative to the source directory that is being built
   222    (the context of the build). The `<dest>` is the absolute path, or path relative
   223    to **WORKDIR**, into which the source is copied inside the target container.
   224    If the `<src>` argument is a local file in a recognized compression format
   225    (tar, gzip, bzip2, etc) then it is unpacked at the specified `<dest>` in the
   226    container's filesystem.  Note that only local compressed files will be unpacked,
   227    i.e., the URL download and archive unpacking features cannot be used together.
   228    All new directories are created with mode 0755 and with the uid and gid of **0**.
   229  
   230  **COPY**
   231    -- **COPY** has two forms:
   232  
   233    ```
   234    COPY <src> <dest>
   235  
   236    # Required for paths with whitespace
   237    COPY ["<src>",... "<dest>"]
   238    ```
   239  
   240    The **COPY** instruction copies new files from `<src>` and
   241    adds them to the filesystem of the container at path <dest>. The `<src>` must be
   242    the path to a file or directory relative to the source directory that is
   243    being built (the context of the build) or a remote file URL. The `<dest>` is an
   244    absolute path, or a path relative to **WORKDIR**, into which the source will
   245    be copied inside the target container. If you **COPY** an archive file it will
   246    land in the container exactly as it appears in the build context without any
   247    attempt to unpack it.  All new files and directories are created with mode **0755**
   248    and with the uid and gid of **0**.
   249  
   250  **ENTRYPOINT**
   251    -- **ENTRYPOINT** has two forms:
   252  
   253    ```
   254    # executable form
   255    ENTRYPOINT ["executable", "param1", "param2"]`
   256  
   257    # run command in a shell - /bin/sh -c
   258    ENTRYPOINT command param1 param2
   259    ```
   260  
   261    -- An **ENTRYPOINT** helps you configure a
   262    container that can be run as an executable. When you specify an **ENTRYPOINT**,
   263    the whole container runs as if it was only that executable.  The **ENTRYPOINT**
   264    instruction adds an entry command that is not overwritten when arguments are
   265    passed to docker run. This is different from the behavior of **CMD**. This allows
   266    arguments to be passed to the entrypoint, for instance `docker run <image> -d`
   267    passes the -d argument to the **ENTRYPOINT**.  Specify parameters either in the
   268    **ENTRYPOINT** JSON array (as in the preferred exec form above), or by using a **CMD**
   269    statement.  Parameters in the **ENTRYPOINT** are not overwritten by the docker run
   270    arguments.  Parameters specified via **CMD** are overwritten by docker run
   271    arguments.  Specify a plain string for the **ENTRYPOINT**, and it will execute in
   272    `/bin/sh -c`, like a **CMD** instruction:
   273  
   274    ```
   275    FROM ubuntu
   276    ENTRYPOINT wc -l -
   277    ```
   278  
   279    This means that the Dockerfile's image always takes stdin as input (that's
   280    what "-" means), and prints the number of lines (that's what "-l" means). To
   281    make this optional but default, use a **CMD**:
   282  
   283    ```
   284    FROM ubuntu
   285    CMD ["-l", "-"]
   286    ENTRYPOINT ["/usr/bin/wc"]
   287    ```
   288  
   289  **VOLUME**
   290    -- `VOLUME ["/data"]`
   291    The **VOLUME** instruction creates a mount point with the specified name and marks
   292    it as holding externally-mounted volumes from the native host or from other
   293    containers.
   294  
   295  **USER**
   296    -- `USER daemon`
   297    Sets the username or UID used for running subsequent commands.
   298  
   299    The **USER** instruction can optionally be used to set the group or GID. The
   300    followings examples are all valid:
   301    USER [user | user:group | uid | uid:gid | user:gid | uid:group ]
   302  
   303    Until the **USER** instruction is set, instructions will be run as root. The USER
   304    instruction can be used any number of times in a Dockerfile, and will only affect
   305    subsequent commands.
   306  
   307  **WORKDIR**
   308    -- `WORKDIR /path/to/workdir`
   309    The **WORKDIR** instruction sets the working directory for the **RUN**, **CMD**,
   310    **ENTRYPOINT**, **COPY** and **ADD** Dockerfile commands that follow it. It can
   311    be used multiple times in a single Dockerfile. Relative paths are defined
   312    relative to the path of the previous **WORKDIR** instruction. For example:
   313  
   314    ```
   315    WORKDIR /a
   316    WORKDIR b
   317    WORKDIR c
   318    RUN pwd
   319    ```
   320  
   321    In the above example, the output of the **pwd** command is **a/b/c**.
   322  
   323  **ARG**
   324     -- ARG <name>[=<default value>]
   325  
   326    The `ARG` instruction defines a variable that users can pass at build-time to
   327    the builder with the `docker build` command using the `--build-arg
   328    <varname>=<value>` flag. If a user specifies a build argument that was not
   329    defined in the Dockerfile, the build outputs a warning.
   330  
   331    ```
   332    [Warning] One or more build-args [foo] were not consumed
   333    ```
   334  
   335    The Dockerfile author can define a single variable by specifying `ARG` once or many
   336    variables by specifying `ARG` more than once. For example, a valid Dockerfile:
   337  
   338    ```
   339    FROM busybox
   340    ARG user1
   341    ARG buildno
   342    ...
   343    ```
   344  
   345    A Dockerfile author may optionally specify a default value for an `ARG` instruction:
   346  
   347    ```
   348    FROM busybox
   349    ARG user1=someuser
   350    ARG buildno=1
   351    ...
   352    ```
   353  
   354    If an `ARG` value has a default and if there is no value passed at build-time, the
   355    builder uses the default.
   356  
   357    An `ARG` variable definition comes into effect from the line on which it is
   358    defined in the `Dockerfile` not from the argument's use on the command-line or
   359    elsewhere.  For example, consider this Dockerfile:
   360  
   361    ```
   362    1 FROM busybox
   363    2 USER ${user:-some_user}
   364    3 ARG user
   365    4 USER $user
   366    ...
   367    ```
   368    A user builds this file by calling:
   369  
   370    ```
   371    $ docker build --build-arg user=what_user Dockerfile
   372    ```
   373  
   374    The `USER` at line 2 evaluates to `some_user` as the `user` variable is defined on the
   375    subsequent line 3. The `USER` at line 4 evaluates to `what_user` as `user` is
   376    defined and the `what_user` value was passed on the command line. Prior to its definition by an
   377    `ARG` instruction, any use of a variable results in an empty string.
   378  
   379    > **Warning:** It is not recommended to use build-time variables for
   380    >  passing secrets like github keys, user credentials etc. Build-time variable
   381    >  values are visible to any user of the image with the `docker history` command.
   382  
   383    You can use an `ARG` or an `ENV` instruction to specify variables that are
   384    available to the `RUN` instruction. Environment variables defined using the
   385    `ENV` instruction always override an `ARG` instruction of the same name. Consider
   386    this Dockerfile with an `ENV` and `ARG` instruction.
   387  
   388    ```
   389    1 FROM ubuntu
   390    2 ARG CONT_IMG_VER
   391    3 ENV CONT_IMG_VER v1.0.0
   392    4 RUN echo $CONT_IMG_VER
   393    ```
   394    Then, assume this image is built with this command:
   395  
   396    ```
   397    $ docker build --build-arg CONT_IMG_VER=v2.0.1 Dockerfile
   398    ```
   399  
   400    In this case, the `RUN` instruction uses `v1.0.0` instead of the `ARG` setting
   401    passed by the user:`v2.0.1` This behavior is similar to a shell
   402    script where a locally scoped variable overrides the variables passed as
   403    arguments or inherited from environment, from its point of definition.
   404  
   405    Using the example above but a different `ENV` specification you can create more
   406    useful interactions between `ARG` and `ENV` instructions:
   407  
   408    ```
   409    1 FROM ubuntu
   410    2 ARG CONT_IMG_VER
   411    3 ENV CONT_IMG_VER ${CONT_IMG_VER:-v1.0.0}
   412    4 RUN echo $CONT_IMG_VER
   413    ```
   414  
   415    Unlike an `ARG` instruction, `ENV` values are always persisted in the built
   416    image. Consider a docker build without the --build-arg flag:
   417  
   418    ```
   419    $ docker build Dockerfile
   420    ```
   421  
   422    Using this Dockerfile example, `CONT_IMG_VER` is still persisted in the image but
   423    its value would be `v1.0.0` as it is the default set in line 3 by the `ENV` instruction.
   424  
   425    The variable expansion technique in this example allows you to pass arguments
   426    from the command line and persist them in the final image by leveraging the
   427    `ENV` instruction. Variable expansion is only supported for [a limited set of
   428    Dockerfile instructions.](#environment-replacement)
   429  
   430    Docker has a set of predefined `ARG` variables that you can use without a
   431    corresponding `ARG` instruction in the Dockerfile.
   432  
   433    * `HTTP_PROXY`
   434    * `http_proxy`
   435    * `HTTPS_PROXY`
   436    * `https_proxy`
   437    * `FTP_PROXY`
   438    * `ftp_proxy`
   439    * `NO_PROXY`
   440    * `no_proxy`
   441  
   442    To use these, simply pass them on the command line using the `--build-arg
   443    <varname>=<value>` flag.
   444  
   445  **ONBUILD**
   446    -- `ONBUILD [INSTRUCTION]`
   447    The **ONBUILD** instruction adds a trigger instruction to an image. The
   448    trigger is executed at a later time, when the image is used as the base for
   449    another build. Docker executes the trigger in the context of the downstream
   450    build, as if the trigger existed immediately after the **FROM** instruction in
   451    the downstream Dockerfile.
   452  
   453    You can register any build instruction as a trigger. A trigger is useful if
   454    you are defining an image to use as a base for building other images. For
   455    example, if you are defining an application build environment or a daemon that
   456    is customized with a user-specific configuration.  
   457  
   458    Consider an image intended as a reusable python application builder. It must
   459    add application source code to a particular directory, and might need a build
   460    script called after that. You can't just call **ADD** and **RUN** now, because
   461    you don't yet have access to the application source code, and it is different
   462    for each application build.
   463  
   464    -- Providing application developers with a boilerplate Dockerfile to copy-paste
   465    into their application is inefficient, error-prone, and
   466    difficult to update because it mixes with application-specific code.
   467    The solution is to use **ONBUILD** to register instructions in advance, to
   468    run later, during the next build stage.
   469  
   470  # HISTORY
   471  *May 2014, Compiled by Zac Dover (zdover at redhat dot com) based on docker.com Dockerfile documentation.
   472  *Feb 2015, updated by Brian Goff (cpuguy83@gmail.com) for readability
   473  *Sept 2015, updated by Sally O'Malley (somalley@redhat.com)
   474  *Oct 2016, updated by Addam Hardy (addam.hardy@gmail.com)