Dockerfile (5) Linux Manual Page
Dockerfile – automate the steps of creating a Docker image
Introduction
The Dockerfile is a configuration file that automates the steps of creating a Docker image. It is similar to a Makefile. Docker reads instructions from the Dockerfile to automate the steps otherwise performed manually to create an image. To build an image, create a file called Dockerfile.
The Dockerfile describes the steps taken to assemble the image. When the Dockerfile has been created, call the docker build command, using the path of directory that contains Dockerfile as the argument.
Synopsis
INSTRUCTION arguments For example: FROM image
Description
A Dockerfile is a file that automates the steps of creating a Docker image. A Dockerfile is similar to a Makefile.
Usage
docker build .
— Runs the steps and commits them, building a final image.
-
docker build -t repository/tag .
— specifies a repository and tag at which to save the new image if the build
Docker re-uses intermediate images whenever possible. This significantly
Format
FROM image
FROM image:tag
FROM image [at] digest
— The FROM instruction sets the base image for subsequent instructions. A
FROM as its first instruction. The image can be any
— FROM must be the first non-comment instruction in Dockerfile.
— FROM may appear multiple times within a single Dockerfile in order to create
FROM command.
— If no tag is given to the FROM instruction, Docker applies the
— If no digest is given to the FROM instruction, Docker applies the
MAINTAINER
MAINTAINER sets the Author field for the generated images.
RUN
RUN has two forms:
-
# the command is run in a shell - /bin/sh -c RUN <command> # Executable form RUN ["executable", "param1", "param2"]
— The RUN instruction executes any commands in a new layer on top of the current
— Layering RUN instructions and generating commits conforms to the core
RUN commands using a base image that does not contain /bin/sh.
Note that the exec form is parsed as a JSON array, which means that you must
CMD
CMD has three forms:
-
# Executable form CMD ["executable", "param1", "param2"]` # Provide default arguments to ENTRYPOINT CMD ["param1", "param2"]` # the command is run in a shell - /bin/sh -c CMD command param1 param2
— There should be only one CMD in a Dockerfile. If more than one CMD is listed, only
CMD takes effect.
CMD is to provide defaults for an executing container.
ENTRYPOINT must be specified.
CMD instruction sets the command to
CMD, the <command> executes in /bin/sh -c:
Note that the exec form is parsed as a JSON array, which means that you must
-
FROM ubuntu CMD echo "This is a test." | wc -
— If you run command without a shell, then you must express the command as a
CMD. All additional parameters must be individually expressed
-
FROM ubuntu CMD ["/usr/bin/wc","--help"]
— To make the container run the same executable every time, use ENTRYPOINT in
CMD.
CMD.
RUN with CMD. RUN runs a command and commits the result.
CMD executes nothing at build time, but specifies the intended command for
LABEL
-
LABEL <key>[ <value>] LABEL <key>[ <value>] ...
The LABEL instruction adds metadata to an image. A LABEL is a
LABEL without a value, simply use an empty
LABEL value, use quotes and
-
LABEL com.example.vendor="ACME Incorporated" LABEL com.example.vendor "ACME Incorporated" LABEL com.example.vendor.is-beta "" LABEL com.example.vendor.is-beta= LABEL com.example.vendor.is-beta=""
An image can have more than one label. To specify multiple labels, separate
Labels are additive including LABELs in FROM images. As the system
To display an image’s labels, use the docker inspect command.
STOPSIGNAL
— STOPSIGNAL <signal>
STOPSIGNAL instruction sets the system call signal that will be sent
SIG, for instance SIGKILL, or an unsigned number that matches a
9. The default is
SIGTERM if not defined.
The image’s default stopsignal can be overridden per container, using the
–stop-signal flag on docker-run(1) and docker-create(1).
EXPOSE
EXPOSE instruction informs Docker that the container listens on the
ENV
ENV instruction sets the environment variable
RUN, ENTRYPOINT, and CMD instructions. This is
ENV persist when a container is run
Note that setting "ENV DEBIAN_FRONTEND=noninteractive" may cause
ADD
ADD has two forms:
-
ADD <src> <dest> # Required for paths with whitespace ADD ["<src>",... "<dest>"]
The ADD instruction copies new files, directories
WORKDIR, into which the source is copied inside the target container.
0.
COPY
COPY has two forms:
-
COPY <src> <dest> # Required for paths with whitespace COPY ["<src>",... "<dest>"]
The COPY instruction copies new files from <src> and
WORKDIR, into which the source will
COPY an archive file it will
0755
0.
ENTRYPOINT
ENTRYPOINT has two forms:
-
# executable form ENTRYPOINT ["executable", "param1", "param2"]` # run command in a shell - /bin/sh -c ENTRYPOINT command param1 param2
— An ENTRYPOINT helps you configure a
ENTRYPOINT,
ENTRYPOINT
CMD. This allows
ENTRYPOINT.
ENTRYPOINT JSON array (as in the preferred exec form above), or by using a CMD
ENTRYPOINT are not overwritten by the docker run
CMD are overwritten by docker run
ENTRYPOINT, and it will execute in
CMD instruction:
-
FROM ubuntu ENTRYPOINT wc -l -
This means that the Dockerfile’s image always takes stdin as input (that’s
CMD:
-
FROM ubuntu CMD ["-l", "-"] ENTRYPOINT ["/usr/bin/wc"]
VOLUME
VOLUME instruction creates a mount point with the specified name and marks
USER
The USER instruction can optionally be used to set the group or GID. The
Until the USER instruction is set, instructions will be run as root. The USER
WORKDIR
WORKDIR instruction sets the working directory for the RUN, CMD,
ENTRYPOINT, COPY and ADD Dockerfile commands that follow it. It can
WORKDIR instruction. For example:
-
WORKDIR /a WORKDIR b WORKDIR c RUN pwd
In the above example, the output of the pwd command is a/b/c.
ARG
The ARG instruction defines a variable that users can pass at build-time to
-
[Warning] One or more build-args [foo] were not consumed
The Dockerfile author can define a single variable by specifying ARG once or many
-
FROM busybox ARG user1 ARG buildno ...
A Dockerfile author may optionally specify a default value for an ARG instruction:
-
FROM busybox ARG user1=someuser ARG buildno=1 ...
If an ARG value has a default and if there is no value passed at build-time, the
An ARG variable definition comes into effect from the line on which it is
-
1 FROM busybox 2 USER ${user:-some_user} 3 ARG user 4 USER $user ...
A user builds this file by calling:
-
$ docker build --build-arg user=what_user Dockerfile
The USER at line 2 evaluates to some_user as the user variable is defined on the
Warning:It is not recommended to use build-time variables for
passing secrets like github keys, user credentials etc. Build-time variable
values are visible to any user of the image with the docker history command.
You can use an ARG or an ENV instruction to specify variables that are
-
1 FROM ubuntu 2 ARG CONT_IMG_VER 3 ENV CONT_IMG_VER=v1.0.0 4 RUN echo $CONT_IMG_VER
Then, assume this image is built with this command:
-
$ docker build --build-arg CONT_IMG_VER=v2.0.1 Dockerfile
In this case, the RUN instruction uses v1.0.0 instead of the ARG setting
Using the example above but a different ENV specification you can create more
-
1 FROM ubuntu 2 ARG CONT_IMG_VER 3 ENV CONT_IMG_VER=${CONT_IMG_VER:-v1.0.0} 4 RUN echo $CONT_IMG_VER
Unlike an ARG instruction, ENV values are always persisted in the built
-
$ docker build Dockerfile
Using this Dockerfile example, CONT_IMG_VER is still persisted in the image but
The variable expansion technique in this example allows you to pass arguments
Docker has a set of predefined ARG variables that you can use without a
-
- •
- HTTP_PROXY
- •
- http_proxy
- •
- HTTPS_PROXY
- •
- https_proxy
- •
- FTP_PROXY
- •
- ftp_proxy
- •
- NO_PROXY
- •
- no_proxy
To use these, pass them on the command line using –build-arg flag, for
-
$ docker build --build-arg HTTPS_PROXY=https://my-proxy.example.com .
ONBUILD
ONBUILD instruction adds a trigger instruction to an image. The
FROM instruction in
You can register any build instruction as a trigger. A trigger is useful if
Consider an image intended as a reusable python application builder. It must
ADD and RUN now, because
— Providing application developers with a boilerplate Dockerfile to copy-paste
ONBUILD to register instructions in advance, to
History
*May 2014, Compiled by Zac Dover (zdover at redhat dot com) based on docker.com Dockerfile documentation. *Feb 2015, updated by Brian Goff (cpuguy83 [at] gmail.com) for readability *Sept 2015, updated by Sally O’Malley (somalley [at] redhat.com) *Oct 2016, updated by Addam Hardy (addam.hardy [at] gmail.com)
