github.com/shiroyuki/docker@v1.9.0/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 include spaces within a **LABEL** value, use quotes and
   167    backslashes as you would in command-line parsing.
   168  
   169    ```
   170    LABEL com.example.vendor="ACME Incorporated"
   171    or
   172    LABEL com.example.vendor "ACME Incorporated"
   173    ```
   174  
   175    An image can have more than one label. To specify multiple labels, separate
   176    each key-value pair by a space. 
   177    
   178    Labels are additive including `LABEL`s in `FROM` images. As the system
   179    encounters and then applies a new label, new `key`s override any previous
   180    labels with identical keys.
   181  
   182    To display an image's labels, use the `docker inspect` command.
   183  
   184  **EXPOSE**
   185    -- `EXPOSE <port> [<port>...]`
   186    The **EXPOSE** instruction informs Docker that the container listens on the
   187    specified network ports at runtime. Docker uses this information to
   188    interconnect containers using links and to set up port redirection on the host
   189    system.
   190  
   191  **ENV**
   192    -- `ENV <key> <value>`
   193    The **ENV** instruction sets the environment variable <key> to
   194    the value `<value>`. This value is passed to all future 
   195    **RUN**, **ENTRYPOINT**, and **CMD** instructions. This is
   196    functionally equivalent to prefixing the command with `<key>=<value>`.  The
   197    environment variables that are set with **ENV** persist when a container is run
   198    from the resulting image. Use `docker inspect` to inspect these values, and
   199    change them using `docker run --env <key>=<value>`.
   200  
   201    Note that setting "`ENV DEBIAN_FRONTEND noninteractive`" may cause
   202    unintended consequences, because it will persist when the container is run
   203    interactively, as with the following command: `docker run -t -i image bash`
   204  
   205  **ADD**
   206    -- **ADD** has two forms:
   207  
   208    ```
   209    ADD <src> <dest>
   210  
   211    # Required for paths with whitespace
   212    ADD ["<src>",... "<dest>"]
   213    ```
   214  
   215    The **ADD** instruction copies new files, directories
   216    or remote file URLs to the filesystem of the container at path `<dest>`.
   217    Multiple `<src>` resources may be specified but if they are files or directories
   218    then they must be relative to the source directory that is being built
   219    (the context of the build). The `<dest>` is the absolute path, or path relative
   220    to **WORKDIR**, into which the source is copied inside the target container.
   221    If the `<src>` argument is a local file in a recognized compression format
   222    (tar, gzip, bzip2, etc) then it is unpacked at the specified `<dest>` in the
   223    container's filesystem.  Note that only local compressed files will be unpacked,
   224    i.e., the URL download and archive unpacking features cannot be used together.
   225    All new directories are created with mode 0755 and with the uid and gid of **0**.
   226  
   227  **COPY**
   228    -- **COPY** has two forms:
   229  
   230    ```
   231    COPY <src> <dest>
   232  
   233    # Required for paths with whitespace
   234    COPY ["<src>",... "<dest>"]
   235    ```
   236  
   237    The **COPY** instruction copies new files from `<src>` and
   238    adds them to the filesystem of the container at path <dest>. The `<src>` must be
   239    the path to a file or directory relative to the source directory that is
   240    being built (the context of the build) or a remote file URL. The `<dest>` is an
   241    absolute path, or a path relative to **WORKDIR**, into which the source will
   242    be copied inside the target container. If you **COPY** an archive file it will
   243    land in the container exactly as it appears in the build context without any 
   244    attempt to unpack it.  All new files and directories are created with mode **0755**
   245    and with the uid and gid of **0**.
   246  
   247  **ENTRYPOINT**
   248    -- **ENTRYPOINT** has two forms:
   249  
   250    ```
   251    # executable form
   252    ENTRYPOINT ["executable", "param1", "param2"]`
   253  
   254    # run command in a shell - /bin/sh -c
   255    ENTRYPOINT command param1 param2
   256    ```
   257  
   258    -- An **ENTRYPOINT** helps you configure a
   259    container that can be run as an executable. When you specify an **ENTRYPOINT**,
   260    the whole container runs as if it was only that executable.  The **ENTRYPOINT**
   261    instruction adds an entry command that is not overwritten when arguments are
   262    passed to docker run. This is different from the behavior of **CMD**. This allows
   263    arguments to be passed to the entrypoint, for instance `docker run <image> -d`
   264    passes the -d argument to the **ENTRYPOINT**.  Specify parameters either in the
   265    **ENTRYPOINT** JSON array (as in the preferred exec form above), or by using a **CMD**
   266    statement.  Parameters in the **ENTRYPOINT** are not overwritten by the docker run
   267    arguments.  Parameters specified via **CMD** are overwritten by docker run
   268    arguments.  Specify a plain string for the **ENTRYPOINT**, and it will execute in
   269    `/bin/sh -c`, like a **CMD** instruction:
   270  
   271    ```
   272    FROM ubuntu
   273    ENTRYPOINT wc -l -
   274    ```
   275  
   276    This means that the Dockerfile's image always takes stdin as input (that's
   277    what "-" means), and prints the number of lines (that's what "-l" means). To
   278    make this optional but default, use a **CMD**:
   279  
   280    ```
   281    FROM ubuntu
   282    CMD ["-l", "-"]
   283    ENTRYPOINT ["/usr/bin/wc"]
   284    ```
   285  
   286  **VOLUME**
   287    -- `VOLUME ["/data"]`
   288    The **VOLUME** instruction creates a mount point with the specified name and marks
   289    it as holding externally-mounted volumes from the native host or from other
   290    containers.
   291  
   292  **USER**
   293    -- `USER daemon`
   294    Sets the username or UID used for running subsequent commands.
   295  
   296    The **USER** instruction can optionally be used to set the group or GID. The
   297    followings examples are all valid:
   298    USER [user | user:group | uid | uid:gid | user:gid | uid:group ]
   299  
   300    Until the **USER** instruction is set, instructions will be run as root. The USER
   301    instruction can be used any number of times in a Dockerfile, and will only affect
   302    subsequent commands.
   303  
   304  **WORKDIR**
   305    -- `WORKDIR /path/to/workdir`
   306    The **WORKDIR** instruction sets the working directory for the **RUN**, **CMD**,
   307    **ENTRYPOINT**, **COPY** and **ADD** Dockerfile commands that follow it. It can
   308    be used multiple times in a single Dockerfile. Relative paths are defined
   309    relative to the path of the previous **WORKDIR** instruction. For example:
   310  
   311    ```
   312    WORKDIR /a
   313    WORKDIR b
   314    WORKDIR c
   315    RUN pwd
   316    ```
   317  
   318    In the above example, the output of the **pwd** command is **a/b/c**.
   319  
   320  **ARG**
   321     -- ARG <name>[=<default value>]
   322  
   323    The `ARG` instruction defines a variable that users can pass at build-time to
   324    the builder with the `docker build` command using the `--build-arg
   325    <varname>=<value>` flag. If a user specifies a build argument that was not
   326    defined in the Dockerfile, the build outputs an error.
   327  
   328    ```
   329    One or more build-args were not consumed, failing build.
   330    ```
   331  
   332    The Dockerfile author can define a single variable by specifying `ARG` once or many
   333    variables by specifying `ARG` more than once. For example, a valid Dockerfile:
   334  
   335    ```
   336    FROM busybox
   337    ARG user1
   338    ARG buildno
   339    ...
   340    ```
   341  
   342    A Dockerfile author may optionally specify a default value for an `ARG` instruction:
   343  
   344    ```
   345    FROM busybox
   346    ARG user1=someuser
   347    ARG buildno=1
   348    ...
   349    ```
   350  
   351    If an `ARG` value has a default and if there is no value passed at build-time, the
   352    builder uses the default.
   353  
   354    An `ARG` variable definition comes into effect from the line on which it is
   355    defined in the `Dockerfile` not from the argument's use on the command-line or
   356    elsewhere.  For example, consider this Dockerfile:
   357  
   358    ```
   359    1 FROM busybox
   360    2 USER ${user:-some_user}
   361    3 ARG user
   362    4 USER $user
   363    ...
   364    ```
   365    A user builds this file by calling:
   366  
   367    ```
   368    $ docker build --build-arg user=what_user Dockerfile
   369    ```
   370  
   371    The `USER` at line 2 evaluates to `some_user` as the `user` variable is defined on the
   372    subsequent line 3. The `USER` at line 4 evaluates to `what_user` as `user` is
   373    defined and the `what_user` value was passed on the command line. Prior to its definition by an
   374    `ARG` instruction, any use of a variable results in an empty string.
   375  
   376    > **Note:** It is not recommended to use build-time variables for
   377    >  passing secrets like github keys, user credentials etc.
   378  
   379    You can use an `ARG` or an `ENV` instruction to specify variables that are
   380    available to the `RUN` instruction. Environment variables defined using the
   381    `ENV` instruction always override an `ARG` instruction of the same name. Consider
   382    this Dockerfile with an `ENV` and `ARG` instruction.
   383  
   384    ```
   385    1 FROM ubuntu
   386    2 ARG CONT_IMG_VER
   387    3 ENV CONT_IMG_VER v1.0.0
   388    4 RUN echo $CONT_IMG_VER
   389    ```
   390    Then, assume this image is built with this command:
   391  
   392    ```
   393    $ docker build --build-arg CONT_IMG_VER=v2.0.1 Dockerfile
   394    ```
   395  
   396    In this case, the `RUN` instruction uses `v1.0.0` instead of the `ARG` setting
   397    passed by the user:`v2.0.1` This behavior is similar to a shell
   398    script where a locally scoped variable overrides the variables passed as
   399    arguments or inherited from environment, from its point of definition.
   400  
   401    Using the example above but a different `ENV` specification you can create more
   402    useful interactions between `ARG` and `ENV` instructions:
   403  
   404    ```
   405    1 FROM ubuntu
   406    2 ARG CONT_IMG_VER
   407    3 ENV CONT_IMG_VER ${CONT_IMG_VER:-v1.0.0}
   408    4 RUN echo $CONT_IMG_VER
   409    ```
   410  
   411    Unlike an `ARG` instruction, `ENV` values are always persisted in the built
   412    image. Consider a docker build without the --build-arg flag:
   413  
   414    ```
   415    $ docker build Dockerfile
   416    ```
   417  
   418    Using this Dockerfile example, `CONT_IMG_VER` is still persisted in the image but
   419    its value would be `v1.0.0` as it is the default set in line 3 by the `ENV` instruction.
   420  
   421    The variable expansion technique in this example allows you to pass arguments
   422    from the command line and persist them in the final image by leveraging the
   423    `ENV` instruction. Variable expansion is only supported for [a limited set of
   424    Dockerfile instructions.](#environment-replacement)
   425  
   426    Docker has a set of predefined `ARG` variables that you can use without a
   427    corresponding `ARG` instruction in the Dockerfile.
   428  
   429    * `HTTP_PROXY`
   430    * `http_proxy`
   431    * `HTTPS_PROXY`
   432    * `https_proxy`
   433    * `FTP_PROXY`
   434    * `ftp_proxy`
   435    * `NO_PROXY`
   436    * `no_proxy`
   437  
   438    To use these, simply pass them on the command line using the `--build-arg
   439    <varname>=<value>` flag.
   440  
   441  **ONBUILD**
   442    -- `ONBUILD [INSTRUCTION]`
   443    The **ONBUILD** instruction adds a trigger instruction to an image. The
   444    trigger is executed at a later time, when the image is used as the base for
   445    another build. Docker executes the trigger in the context of the downstream
   446    build, as if the trigger existed immediately after the **FROM** instruction in
   447    the downstream Dockerfile.
   448  
   449    You can register any build instruction as a trigger. A trigger is useful if
   450    you are defining an image to use as a base for building other images. For
   451    example, if you are defining an application build environment or a daemon that
   452    is customized with a user-specific configuration.  
   453    
   454    Consider an image intended as a reusable python application builder. It must
   455    add application source code to a particular directory, and might need a build
   456    script called after that. You can't just call **ADD** and **RUN** now, because
   457    you don't yet have access to the application source code, and it is different
   458    for each application build.
   459  
   460    -- Providing application developers with a boilerplate Dockerfile to copy-paste
   461    into their application is inefficient, error-prone, and
   462    difficult to update because it mixes with application-specific code.
   463    The solution is to use **ONBUILD** to register instructions in advance, to
   464    run later, during the next build stage.
   465  
   466  # HISTORY
   467  *May 2014, Compiled by Zac Dover (zdover at redhat dot com) based on docker.com Dockerfile documentation.
   468  *Feb 2015, updated by Brian Goff (cpuguy83@gmail.com) for readability
   469  *Sept 2015, updated by Sally O'Malley (somalley@redhat.com)