您的位置:首页 > 运维架构 > Docker

Best Practices for Writing Dockerfiles

2015-12-29 16:58 741 查看

Best practices for writing Dockerfiles

Overview

Docker can build images automatically by reading the instructions from a
Dockerfile
, a text file that contains all the commands, in order, needed tobuild a given image.
Dockerfile
s adhere to a specific format and use aspecific set of instructions. You can learn the basics on theDockerfile Reference page. Ifyou’re new to writing
Dockerfile
s, you should start there.

This document covers the best practices and methods recommended by Docker,Inc. and the Docker community for creating easy-to-use, effective
Dockerfile
s. We strongly suggest you follow these recommendations (in fact,if you’re creating an Official
Image, you must adhere to these practices).

You can see many of these practices and recommendations in action in the
buildpack-deps
Dockerfile
.

Note: for more detailed explanations of any of the Dockerfile commandsmentioned 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 asephemeral as possible. By “ephemeral,” we mean that it can be stopped anddestroyed and a new one built and put in place with an absolute minimum ofset-up and configuration.

Use a .dockerignore file

In most cases, it’s best to put each Dockerfile in an empty directory. Then,add to that directory only the files needed for building the Dockerfile. Toincrease the build’s performance, you can exclude files and directories byadding a
.dockerignore
file to that directory as well. This file supportsexclusion patterns similar to
.gitignore
files. For information on creating one,see the
.dockerignore file.

Avoid installing unnecessary packages

In order to reduce complexity, dependencies, file sizes, and build times, youshould avoid installing extra or unnecessary packages just because theymight be “nice to have.” For example, you don’t need to include a text editorin a database image.

Run only one process per container

In almost all cases, you should only run a single process in a singlecontainer. Decoupling applications into multiple containers makes it mucheasier to scale horizontally and reuse containers. If that service depends onanother service, make use of

container linking.

Minimize the number of layers

You need to find the balance between readability (and thus long-termmaintainability) of the
Dockerfile
and minimizing the number of layers ituses. Be strategic and cautious about the number of layers you use.

Sort multi-line arguments

Whenever possible, ease later changes by sorting multi-line argumentsalphanumerically. This will help you avoid duplication of packages and make thelist much easier to update. This also makes PRs a lot easier to read andreview. Adding a space before a backslash
(
\
) helps as well.

Here’s an example from the
buildpack-deps
image:

RUN apt-get update && apt-get install -y \
bzr \
cvs \
git \
mercurial \
subversion

Build cache

During the process of building an image Docker will step through theinstructions in your
Dockerfile
executing each in the order specified.As each instruction is examined Docker will look for an existing image in itscache 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
--no-cache=true
option on the
docker build
command.

However, if you do let Docker use its cache then it is very important tounderstand when it will, and will not, find a matching image. The basic rulesthat Docker will follow are outlined below:

Starting with a base image that is already in the cache, the nextinstruction is compared against all child images derived from that baseimage to see if one of them was built using the exact same instruction. Ifnot, the cache is invalidated.

In most cases simply comparing the instruction in the
Dockerfile
with oneof the child images is sufficient. However, certain instructions requirea little more examination and explanation.

For the
ADD
and
COPY
instructions, 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 inthese checksums. During
the cache lookup, the checksum is compared against thechecksum in the existing images. If anything has changed in the file(s), suchas the contents and metadata, then the cache is invalidated.

Aside from the
ADD
and
COPY
commands, cache checking will not look at thefiles in the container to determine a cache match. For example, when processinga
RUN apt-get -y update
command the files updated in the containerwill not be examined to determine if a cache hit exists. In that case justthe command string itself will be used to find a match.

Once the cache is invalidated, all subsequent
Dockerfile
commands willgenerate new images and the cache will not be used.

The Dockerfile instructions

Below you’ll find recommendations for the best way to write thevarious instructions available for use in a
Dockerfile
.

FROM

Dockerfile reference for the FROM instruction

Whenever possible, use current Official Repositories as the basis for yourimage. We recommend the
Debian imagesince it’s very tightly controlled and kept extremely minimal (currently under100 mb), while still being a full distribution.

RUN

Dockerfile reference for the RUN instruction

As always, to make your
Dockerfile
more readable, understandable, andmaintainable, split long or complex
RUN
statements on multiple lines separatedwith backslashes.

apt-get

Probably the most common use-case for
RUN
is an application of
apt-get
. The
RUN apt-get
command, because it installs packages, has several gotchas to lookout for.

You should avoid
RUN apt-get upgrade
or
dist-upgrade
, as many of the“essential” packages from the base images won’t upgrade inside an unprivilegedcontainer. If a package contained in the base image is out-of-date, you shouldcontact
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.

Always combine
RUN apt-get update
with
apt-get install
in the same
RUN
statement, for example:

RUN apt-get update && apt-get install -y \
package-bar \
package-baz \
package-foo

Using
apt-get update
alone in a
RUN
statement causes caching issues andsubsequent
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 latermodify
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 thecache from previous steps. As a result the
apt-get update
is NOT executedbecause the build uses the cached version. Because the
apt-get update
is notrun, your build can potentially get an outdated version of the
curl
and
nginx
packages.

Using
RUN apt-get update && apt-get install -y
ensures your Dockerfileinstalls the latest package versions with no further coding or manualintervention. This technique is known as “cache busting”. You can also achievecache-busting by specifying
a package version. This is known as version pinning,for example:

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 ofwhat’s in the cache. This technique can also reduce failures due to unanticipated changesin required packages.

Below is a well-formed
RUN
instruction that demonstrates all the
apt-get
recommendations.

RUN apt-get update && apt-get install -y \
aufs-tools \
automake \
build-essential \
curl \
dpkg-sig \
libcap-dev \
libsqlite3-dev \
lxc=1.0* \
mercurial \
reprepro \
ruby1.9.1 \
ruby1.9.1-dev \
s3cmd=1.1.* \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

The
s3cmd
instructions specifies a version
1.1.0*
. If the image previouslyused an older version, specifying the new one causes a cache bust of
apt-getupdate
and ensure the installation of the new version. Listing packages oneach line can also prevent mistakes in package duplication.

In addition, cleaning up the apt cache and removing
/var/lib/apt/lists
helpskeep the image size down. Since the
RUN
statement starts with
apt-get update
, the package cache will always be refreshed prior to
apt-get install
.

CMD

Dockerfile reference for the CMD instruction

The
CMD
instruction should be used to run the software contained by yourimage, along with any arguments.
CMD
should almost always be used in theform of
CMD [“executable”, “param1”, “param2”…]
. Thus, if the image is for aservice (Apache, Rails, etc.), you would run something like
CMD ["apache2","-DFOREGROUND"]
. Indeed, this
form of the instruction isrecommended for any service-based image.

In most other cases,
CMD
should be given an interactive shell (bash, python,perl, etc), 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”]
inconjunction with

ENTRYPOINT
, unlessyou and your expected users are already quite familiar with how
ENTRYPOINT
works.

EXPOSE

Dockerfile reference for the EXPOSE instruction

The
EXPOSE
instruction indicates the ports on which a container will listenfor connections. Consequently, you should use the common, traditional port foryour application. For example, an image containing the Apache web server woulduse
EXPOSE 80
, while an image containing MongoDB would use
EXPOSE 27017
andso on.

For external access, your users can execute
docker run
with a flag indicatinghow to map the specified port to the port of their choice.For container linking, Docker provides environment variables for the path fromthe recipient container back
to the source (ie,
MYSQL_PORT_3306_TCP
).

ENV

Dockerfile reference for the ENV instruction

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. Forexample,
ENV PATH /usr/local/nginx/bin:$PATH
will ensure that
CMD [“nginx”]
just works.

The
ENV
instruction is also useful for providing required environmentvariables specific to services you wish to containerize, such as Postgres’s
PGDATA
.

Lastly,
ENV
can also be used to set commonly used version numbers so thatversion 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-codingvalues), this approach lets you change a single
ENV
instruction toauto-magically bump the version of the software in your container.

ADD or COPY

Dockerfile reference for the ADD instruction

Dockerfile reference for the COPY instruction

Although
ADD
and
COPY
are functionally similar, generally speaking,
COPY
is preferred. That’s because it’s more transparent than
ADD
.
COPY
onlysupports the basic copying of local files into the container, while
ADD
hassome features (like local-only tar extraction and remote URL support) that arenot immediately obvious. Consequently, the best use for
ADD
is local tar fileauto-extraction into the image, as in
ADD rootfs.tar.xz /
.

If you have multiple
Dockerfile
steps that use different files from yourcontext,
COPY
them individually, rather than all at once. This will ensure thateach step’s build cache is only invalidated (forcing the step to be re-run) if thespecifically required files change.

For example:

COPY requirements.txt /tmp/
RUN pip install /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 isstrongly discouraged; you should use
curl
or
wget
instead. That way you candelete the files you no longer need after they’ve been extracted and you won’thave to add another layer in your image. For example, you should avoid doingthings like:

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
ADD
’s tarauto-extraction capability, you should always use
COPY
.

ENTRYPOINT

Dockerfile reference for the ENTRYPOINT instruction

The best use for
ENTRYPOINT
is to set the image’s main command, allowing thatimage to be run as though it was that command (and then use
CMD
as thedefault flags).

Let’s start with an example of an image for the command line tool
s3cmd
:

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 asshown in the command above.

The
ENTRYPOINT
instruction can also be used in combination with a helperscript, allowing it to function in a similar way to the command above, evenwhen starting the tool may require more than one step.

For example, the
Postgres Official Imageuses the following script as its
ENTRYPOINT
:

#!/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
exec
Bash commandso that the final running application becomes the container’s PID 1. This allowsthe application to receive any Unix signals sent to the container.See the

ENTRYPOINT
help for more details.

The helper script is copied into the container and run via
ENTRYPOINT
oncontainer start:

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

Dockerfile reference for the VOLUME instruction

The
VOLUME
instruction should be used to expose any database storage area,configuration storage, or files/folders created by your docker container. Youare strongly encouraged to use
VOLUME
for any mutable and/or user-serviceableparts of your image.

USER

Dockerfile reference for the USER instruction

If a service can run without privileges, use
USER
to change to a non-rootuser. Start by creating the user and group in the
Dockerfile
with somethinglike
RUN groupadd -r postgres && useradd -r -g postgres postgres
.

Note: Users and groups in an image get a non-deterministicUID/GID in that the “next” UID/GID gets assigned regardless of imagerebuilds. So, if it’s critical, you should assign an explicit UID/GID.

You should avoid installing or using
sudo
since it has unpredictable TTY andsignal-forwarding behavior that can cause more problems than it solves. Ifyou absolutely need functionality similar to
sudo
(e.g., initializing thedaemon as root but running it as non-root), you may be able to use“gosu”.

Lastly, to reduce layers and complexity, avoid switching
USER
backand forth frequently.

WORKDIR

Dockerfile reference for the WORKDIR instruction

For clarity and reliability, you should always use absolute paths for your
WORKDIR
. Also, you should use
WORKDIR
instead of proliferatinginstructions like
RUN cd … && do-something
, which are hard to read,troubleshoot, and maintain.

ONBUILD

Dockerfile reference for the ONBUILD instruction

An
ONBUILD
command executes after the current
Dockerfile
build completes.
ONBUILD
executes in any child image derived
FROM
the current image. Thinkof the
ONBUILD
command as an instruction the parent
Dockerfile
givesto the child
Dockerfile
.

A Docker build executes
ONBUILD
commands before any command in a child
Dockerfile
.

ONBUILD
is useful for images that are going to be built
FROM
a givenimage. For example, you would use
ONBUILD
for a language stack image thatbuilds arbitrary user software written in that language within the
Dockerfile
, as you can see in

Ruby’s
ONBUILD
variants.

Images built from
ONBUILD
should get a separate tag, for example:
ruby:1.9-onbuild
or
ruby:2.0-onbuild
.

Be careful when putting
ADD
or
COPY
in
ONBUILD
. The “onbuild” image willfail catastrophically if the new build’s context is missing the resource beingadded. Adding a separate tag, as recommended above, will help mitigate
this byallowing the
Dockerfile
author to make a choice.

Examples for Official Repositories

These Official Repositories have exemplary
Dockerfile
s:

Go
Perl
Hy
Rails

Additional resources:

Dockerfile Reference
More about Base Images
More about Automated Builds
Guidelines for Creating OfficialRepositories
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: