Best practices for writing DockerfilesEstimated reading time: 22 minutes
Docker can build images automatically by reading the instructions from a
Dockerfile, a text file that contains all the commands, in order, needed to
build a given image.
Dockerfiles adhere to a specific format and use a
specific set of instructions. You can learn the basics on the
Dockerfile Reference page. If
you’re new to writing
Dockerfiles, you should start there.
This document covers the best practices and methods recommended by Docker, Inc. and the Docker community for building efficient images. To see many of these practices and recommendations in action, check out the Dockerfile for buildpack-deps.
Note: for more detailed explanations of any of the Dockerfile commands mentioned here, visit the Dockerfile Reference page.
General guidelines and recommendations
Containers should be ephemeral
The container produced by the image your
Dockerfile defines should be as
ephemeral as possible. By “ephemeral,” we mean that it can be stopped and
destroyed and a new one built and put in place with an absolute minimum of
set-up and configuration. You may want to take a look at the
Processes section of the 12 Factor app
methodology to get a feel for the motivations of running containers in such a
Use a .dockerignore file
The current working directory where you are located when you issue a
docker build command is called the build context, and the
be somewhere within this build context. By default, it is assumed to be in the
current directory, but you can specify a different location by using the
flag. Regardless of where the
Dockerfile actually lives, all of the recursive
contents of files and directories in the current directory are sent to the
Docker daemon as the build context. Inadvertently including files that are not
necessary for building the image results in a larger build context and larger
image size. These in turn can increase build time, time to pull and push the
image, and the runtime size of containers. To see how big your build context
is, look for a message like the following, when you build your
Sending build context to Docker daemon 187.8MB
To exclude files which are not relevant to the build, without restructuring your
source repository, use a
.dockerignore file. This file supports
exclusion patterns similar to
.gitignore files. For information on creating
one, see the .dockerignore file.
In addition to using a
.dockerignore file, check out the information below
on multi-stage builds.
Use multi-stage builds
If you use Docker 17.05 or higher, you can use multi-stage builds to drastically reduce the size of your final image, without the need to jump through hoops to reduce the number of intermediate layers or remove intermediate files during the build.
Avoid installing unnecessary packages
In order to reduce complexity, dependencies, file sizes, and build times, you should avoid installing extra or unnecessary packages just because they might be “nice to have.” For example, you don’t need to include a text editor in a database image.
Each container should have only one concern
Decoupling applications into multiple containers makes it much easier to scale horizontally and reuse containers. For instance, a web application stack might consist of three separate containers, each with its own unique image, to manage the web application, database, and an in-memory cache in a decoupled manner.
You may have heard that there should be “one process per container”. While this mantra has good intentions, it is not necessarily true that there should be only one operating system process per container. In addition to the fact that containers can now be spawned with an init process, some programs might spawn additional processes of their own accord. For instance, Celery can spawn multiple worker processes, or Apache might create a process per request. While “one process per container” is frequently a good rule of thumb, it is not a hard and fast rule. Use your best judgment to keep containers as clean and modular as possible.
If containers depend on each other, you can use Docker container networks to ensure that these containers can communicate.
Minimize the number of layers
You need to find the balance between readability (and thus long-term
maintainability) of the
Dockerfile and minimizing the number of layers it
uses. Be strategic and cautious about the number of layers you use.
Sort multi-line arguments
Whenever possible, ease later changes by sorting multi-line arguments
alphanumerically. This will help you avoid duplication of packages and make the
list much easier to update. This also makes PRs a lot easier to read and
review. Adding a space before a backslash (
\) helps as well.
Here’s an example from the
RUN apt-get update && apt-get install -y \ bzr \ cvs \ git \ mercurial \ subversion
During the process of building an image Docker will step through the
instructions in your
Dockerfile executing each in the order specified.
As each instruction is examined Docker will look for an existing image in its
cache that it can reuse, rather than creating a new (duplicate) image.
If you do not want to use the cache at all you can use the
option on the
docker build command.
However, if you do let Docker use its cache then it is very important to understand when it will, and will not, find a matching image. The basic rules that Docker will follow are outlined below:
Starting with a parent image that is already in the cache, the next instruction is compared against all child images derived from that base image to see if one of them was built using the exact same instruction. If not, the cache is invalidated.
In most cases simply comparing the instruction in the
Dockerfilewith one of the child images is sufficient. However, certain instructions require a little more examination and explanation.
COPYinstructions, the contents of the file(s) in the image are examined and a checksum is calculated for each file. The last-modified and last-accessed times of the file(s) are not considered in these checksums. During the cache lookup, the checksum is compared against the checksum in the existing images. If anything has changed in the file(s), such as the contents and metadata, then the cache is invalidated.
Aside from the
COPYcommands, cache checking will not look at the files in the container to determine a cache match. For example, when processing a
RUN apt-get -y updatecommand the files updated in the container will not be examined to determine if a cache hit exists. In that case just the command string itself will be used to find a match.
Once the cache is invalidated, all subsequent
Dockerfile commands will
generate new images and the cache will not be used.
The Dockerfile instructions
Below you’ll find recommendations for the best way to write the
various instructions available for use in a
Whenever possible, use current Official Repositories as the basis for your image. We recommend the Debian image since it’s very tightly controlled and kept minimal (currently under 150 mb), while still being a full distribution.
You can add labels to your image to help organize images by project, record
licensing information, to aid in automation, or for other reasons. For each
label, add a line beginning with
LABEL and with one or more key-value pairs.
The following examples show the different acceptable formats. Explanatory comments are included inline.
Note: If your string contains spaces, it must be quoted or the spaces must be escaped. If your string contains inner quote characters (
"), escape them as well.
# Set one or more individual labels LABEL com.example.version="0.0.1-beta" LABEL vendor="ACME Incorporated" LABEL com.example.release-date="2015-02-12" LABEL com.example.version.is-production=""
An image can have more than one label. To specify multiple labels, Docker
recommends combining labels into a single
LABEL instruction where possible.
LABEL instruction produces a new layer which can result in an inefficient
image if you use many labels. This example results in a single image layer.
# Set multiple labels on one line LABEL com.example.version="0.0.1-beta" com.example.release-date="2015-02-12"
The above can also be written as:
# Set multiple labels at once, using line-continuation characters to break long lines LABEL vendor=ACME\ Incorporated \ com.example.is-beta= \ com.example.is-production="" \ com.example.version="0.0.1-beta" \ com.example.release-date="2015-02-12"
See Understanding object labels for guidelines about acceptable label keys and values. For information about querying labels, refer to the items related to filtering in Managing labels on objects. See also LABEL in the Dockerfile reference.
As always, to make your
Dockerfile more readable, understandable, and
maintainable, split long or complex
RUN statements on multiple lines separated
Probably the most common use-case for
RUN is an application of
RUN apt-get command, because it installs packages, has several gotchas to look
You should avoid
RUN apt-get upgrade or
dist-upgrade, as many of the
“essential” packages from the parent images won’t upgrade inside an unprivileged
container. If a package contained in the parent image is out-of-date, you should
contact its maintainers.
If you know there’s a particular package,
foo, that needs to be updated, use
apt-get install -y foo to update automatically.
RUN apt-get update with
apt-get install in the same
statement, for example:
RUN apt-get update && apt-get install -y \ package-bar \ package-baz \ package-foo
apt-get update alone in a
RUN statement causes caching issues and
apt-get install instructions fail.
For example, say you have a Dockerfile:
FROM ubuntu:14.04 RUN apt-get update RUN apt-get install -y curl
After building the image, all layers are in the Docker cache. Suppose you later
apt-get install by adding extra package:
FROM ubuntu:14.04 RUN apt-get update RUN apt-get install -y curl nginx
Docker sees the initial and modified instructions as identical and reuses the
cache from previous steps. As a result the
apt-get update is NOT executed
because the build uses the cached version. Because the
apt-get update is not
run, your build can potentially get an outdated version of the
RUN apt-get update && apt-get install -y ensures your Dockerfile
installs the latest package versions with no further coding or manual
intervention. This technique is known as “cache busting”. You can also achieve
cache-busting by specifying a package version. This is known as version pinning,
RUN apt-get update && apt-get install -y \ package-bar \ package-baz \ package-foo=1.3.*
Version pinning forces the build to retrieve a particular version regardless of what’s in the cache. This technique can also reduce failures due to unanticipated changes in required packages.
Below is a well-formed
RUN instruction that demonstrates all the
RUN apt-get update && apt-get install -y \ aufs-tools \ automake \ build-essential \ curl \ dpkg-sig \ libcap-dev \ libsqlite3-dev \ mercurial \ reprepro \ ruby1.9.1 \ ruby1.9.1-dev \ s3cmd=1.1.* \ && rm -rf /var/lib/apt/lists/*
s3cmd instructions specifies a version
1.1.*. If the image previously
used an older version, specifying the new one causes a cache bust of
update and ensure the installation of the new version. Listing packages on
each line can also prevent mistakes in package duplication.
In addition, when you clean up the apt cache by removing
reduces the image size, since the apt cache is not stored in a layer. Since the
RUN statement starts with
apt-get update, the package cache will always be
refreshed prior to
Note: The official Debian and Ubuntu images automatically run
apt-get clean, so explicit invocation is not required.
RUN commands depend on the ability to pipe the output of one command into another, using the pipe character (
|), as in the following example:
RUN wget -O - https://some.site | wc -l > /number
Docker executes these commands using the
/bin/sh -c interpreter, which
only evaluates the exit code of the last operation in the pipe to determine
success. In the example above this build step succeeds and produces a new
image so long as the
wc -l command succeeds, even if the
If you want the command to fail due to an error at any stage in the pipe,
set -o pipefail && to ensure that an unexpected error prevents
the build from inadvertently succeeding. For example:
RUN set -o pipefail && wget -O - https://some.site | wc -l > /number
Note: Not all shells support the
-o pipefailoption. In such cases (such as the
dashshell, which is the default shell on Debian-based images), consider using the exec form of
RUNto explicitly choose a shell that does support the
pipefailoption. For example:
RUN ["/bin/bash", "-c", "set -o pipefail && wget -O - https://some.site | wc -l > /number"]
CMD instruction should be used to run the software contained by your
image, along with any arguments.
CMD should almost always be used in the
CMD [“executable”, “param1”, “param2”…]. Thus, if the image is for a
service, such as Apache and Rails, you would run something like
CMD ["apache2","-DFOREGROUND"]. Indeed, this form of the instruction is
recommended for any service-based image.
In most other cases,
CMD should be given an interactive shell, such as bash, python
and perl. For example,
CMD ["perl", "-de0"],
CMD ["python"], or
CMD [“php”, “-a”]. Using this form means that when you execute something like
docker run -it python, you’ll get dropped into a usable shell, ready to go.
CMD should rarely be used in the manner of
CMD [“param”, “param”] in
you and your expected users are already quite familiar with how
EXPOSE instruction indicates the ports on which a container will listen
for connections. Consequently, you should use the common, traditional port for
your application. For example, an image containing the Apache web server would
EXPOSE 80, while an image containing MongoDB would use
EXPOSE 27017 and
For external access, your users can execute
docker run with a flag indicating
how to map the specified port to the port of their choice.
For container linking, Docker provides environment variables for the path from
the recipient container back to the source (ie,
In order to make new software easier to run, you can use
ENV to update the
PATH environment variable for the software your container installs. For
ENV PATH /usr/local/nginx/bin:$PATH will ensure that
ENV instruction is also useful for providing required environment
variables specific to services you wish to containerize, such as Postgres’s
ENV can also be used to set commonly used version numbers so that
version bumps are easier to maintain, as seen in the following example:
ENV PG_MAJOR 9.3 ENV PG_VERSION 9.3.4 RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress && … ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH
Similar to having constant variables in a program (as opposed to hard-coding
values), this approach lets you change a single
ENV instruction to
auto-magically bump the version of the software in your container.
ADD or COPY
COPY are functionally similar, generally speaking,
is preferred. That’s because it’s more transparent than
supports the basic copying of local files into the container, while
some features (like local-only tar extraction and remote URL support) that are
not immediately obvious. Consequently, the best use for
ADD is local tar file
auto-extraction into the image, as in
ADD rootfs.tar.xz /.
If you have multiple
Dockerfile steps that use different files from your
COPY them individually, rather than all at once. This will ensure that
each step’s build cache is only invalidated (forcing the step to be re-run) if the
specifically required files change.
COPY requirements.txt /tmp/ RUN pip install --requirement /tmp/requirements.txt COPY . /tmp/
Results in fewer cache invalidations for the
RUN step, than if you put the
COPY . /tmp/ before it.
Because image size matters, using
ADD to fetch packages from remote URLs is
strongly discouraged; you should use
wget instead. That way you can
delete the files you no longer need after they’ve been extracted and you won’t
have to add another layer in your image. For example, you should avoid doing
ADD http://example.com/big.tar.xz /usr/src/things/ RUN tar -xJf /usr/src/things/big.tar.xz -C /usr/src/things RUN make -C /usr/src/things all
And instead, do something like:
RUN mkdir -p /usr/src/things \ && curl -SL http://example.com/big.tar.xz \ | tar -xJC /usr/src/things \ && make -C /usr/src/things all
For other items (files, directories) that do not require
auto-extraction capability, you should always use
The best use for
ENTRYPOINT is to set the image’s main command, allowing that
image to be run as though it was that command (and then use
CMD as the
Let’s start with an example of an image for the command line tool
ENTRYPOINT ["s3cmd"] CMD ["--help"]
Now the image can be run like this to show the command’s help:
$ docker run s3cmd
Or using the right parameters to execute a command:
$ docker run s3cmd ls s3://mybucket
This is useful because the image name can double as a reference to the binary as shown in the command above.
ENTRYPOINT instruction can also be used in combination with a helper
script, allowing it to function in a similar way to the command above, even
when starting the tool may require more than one step.
For example, the Postgres Official Image
uses the following script as its
#!/bin/bash set -e if [ "$1" = 'postgres' ]; then chown -R postgres "$PGDATA" if [ -z "$(ls -A "$PGDATA")" ]; then gosu postgres initdb fi exec gosu postgres "$@" fi exec "$@"
Note: This script uses the
execBash command so that the final running application becomes the container’s PID 1. This allows the application to receive any Unix signals sent to the container. See the
ENTRYPOINThelp for more details.
The helper script is copied into the container and run via
COPY ./docker-entrypoint.sh / ENTRYPOINT ["/docker-entrypoint.sh"]
This script allows the user to interact with Postgres in several ways.
It can simply start Postgres:
$ docker run postgres
Or, it can be used to run Postgres and pass parameters to the server:
$ docker run postgres postgres --help
Lastly, it could also be used to start a totally different tool, such as Bash:
$ docker run --rm -it postgres bash
VOLUME instruction should be used to expose any database storage area,
configuration storage, or files/folders created by your docker container. You
are strongly encouraged to use
VOLUME for any mutable and/or user-serviceable
parts of your image.
If a service can run without privileges, use
USER to change to a non-root
user. Start by creating the user and group in the
Dockerfile with something
RUN groupadd -r postgres && useradd --no-log-init -r -g postgres postgres.
Note: Users and groups in an image get a non-deterministic UID/GID in that the “next” UID/GID gets assigned regardless of image rebuilds. So, if it’s critical, you should assign an explicit UID/GID.
Note: Due to an unresolved bug in the Go archive/tar package’s handling of sparse files, attempting to create a user with a sufficiently large UID inside a Docker container can lead to disk exhaustion as
/var/log/faillogin the container layer is filled with NUL (\0) characters. Passing the
--no-log-initflag to useradd works around this issue. The Debian/Ubuntu
adduserwrapper does not support the
--no-log-initflag and should be avoided.
You should avoid installing or using
sudo since it has unpredictable TTY and
signal-forwarding behavior that can cause more problems than it solves. If
you absolutely need functionality similar to
sudo (e.g., initializing the
daemon as root but running it as non-root), you may be able to use
Lastly, to reduce layers and complexity, avoid switching
and forth frequently.
For clarity and reliability, you should always use absolute paths for your
WORKDIR. Also, you should use
WORKDIR instead of proliferating
RUN cd … && do-something, which are hard to read,
troubleshoot, and maintain.
ONBUILD command executes after the current
Dockerfile build completes.
ONBUILD executes in any child image derived
FROM the current image. Think
ONBUILD command as an instruction the parent
to the child
A Docker build executes
ONBUILD commands before any command in a child
ONBUILD is useful for images that are going to be built
FROM a given
image. For example, you would use
ONBUILD for a language stack image that
builds arbitrary user software written in that language within the
Dockerfile, as you can see in Ruby’s
Images built from
ONBUILD should get a separate tag, for example:
Be careful when putting
ONBUILD. The “onbuild” image will
fail catastrophically if the new build’s context is missing the resource being
added. Adding a separate tag, as recommended above, will help mitigate this by
Dockerfile author to make a choice.
Examples for Official Repositories
These Official Repositories have exemplary
- Dockerfile Reference
- More about Base Images
- More about Automated Builds
- Guidelines for Creating Official Repositories