Building best practices (2024)

Tags

Best practices

Table of contents

Multi-stage builds let you reduce the size of your final image, by creating acleaner separation between the building of your image and the final output.Split your Dockerfile instructions into distinct stages to make sure that theresulting output only contains the files that's needed to run the application.

Using multiple stages can also let you build more efficiently by executingbuild steps in parallel.

SeeMulti-stage builds for moreinformation.

Create reusable stages

If you have multiple images with a lot in common, consider creating a reusablestage that includes the shared components, and basing your unique stages onthat. Docker only needs to build the common stage once. This means that your derivative images use memoryon the Docker host more efficiently and load more quickly.

It's also easier to maintain a common base stage ("Don't repeat yourself"),than it is to have multiple different stages doing similar things.

Choose the right base image

The first step towards achieving a secure image is to choose the right baseimage. When choosing an image, ensure it's built from a trusted source and keepit small.

  • Docker Official Imagesare some of the most secure and dependable images on Docker Hub. Typically,Docker Official images have few or no packages containing CVEs, and arethoroughly reviewed by Docker and project maintainers.

  • Verified Publisher imagesare high-quality images published and maintained by the organizationspartnering with Docker, with Docker verifying the authenticity of the contentin their repositories.

  • Docker-Sponsored Open Sourceare published and maintained by open source projects sponsored by Dockerthrough anopen source program.

When you pick your base image, look out for the badges indicating that theimage is part of these programs.

Building best practices (1)

When building your own image from a Dockerfile, ensure you choose a minimal baseimage that matches your requirements. A smaller base image not only offersportability and fast downloads, but also shrinks the size of your image andminimizes the number of vulnerabilities introduced through the dependencies.

You should also consider using two types of base image: one for building andunit testing, and another (typically slimmer) image for production. In thelater stages of development, your image may not require build tools such ascompilers, build systems, and debugging tools. A small image with minimaldependencies can considerably lower the attack surface.

Docker images are immutable. Building an image is taking a snapshot of thatimage at that moment. That includes any base images, libraries, or othersoftware you use in your build. To keep your images up-to-date and secure, makesure to rebuild your image often, with updated dependencies.

To ensure that you're getting the latest versions of dependencies in your build,you can use the --no-cache option to avoid cache hits.

$ docker build --no-cache -t my-image:my-tag .

The following Dockerfile uses the 24.04 tag of the ubuntu image. Over time,that tag may resolve to a different underlying version of the ubuntu image,as the publisher rebuilds the image with new security patches and updatedlibraries. Using the --no-cache, you can avoid cache hits and ensure a freshdownload of base images and dependencies.

# syntax=docker/dockerfile:1FROM ubuntu:24.04RUN apt-get -y update && apt-get install -y python

Also considerpinning base image versions.

Exclude with .dockerignore

To exclude files not relevant to the build, without restructuring your sourcerepository, use a .dockerignore file. This file supports exclusion patternssimilar to .gitignore files. For information on creating one, seeDockerignore file.

The image defined by your Dockerfile should generate containers that are asephemeral as possible. Ephemeral means that the container can be stoppedand destroyed, then rebuilt and replaced with an absolute minimum set up andconfiguration.

Refer toProcesses under The Twelve-factor Appmethodology to get a feel for the motivations of running containers in such astateless fashion.

Don't install unnecessary packages

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.

When you avoid installing extra or unnecessary packages, your images have reduced complexity, reduced dependencies, reduced file sizes, and reduced build times.

Each container should have only one concern. Decoupling applications intomultiple containers makes it easier to scale horizontally and reuse containers.For instance, a web application stack might consist of three separatecontainers, each with its own unique image, to manage the web application,database, and an in-memory cache in a decoupled manner.

Limiting each container to one process is a good rule of thumb, but it's not ahard and fast rule. For example, not only can containers bespawned with an init process,some programs might spawn additional processes of their own accord. Forinstance,Celery can spawn multiple workerprocesses, andApache can create one process perrequest.

Use your best judgment to keep containers as clean and modular as possible. Ifcontainers depend on each other, you can use Docker container networksto ensure that these containers can communicate.

Sort multi-line arguments

Whenever possible, sort multi-line arguments alphanumerically to make maintenance easier.This helps to 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 thebuildpack-deps image:

RUN apt-get update && apt-get install -y \ bzr \ cvs \ git \ mercurial \ subversion \ && rm -rf /var/lib/apt/lists/*

When building an image, Docker steps through the instructions in yourDockerfile, executing each in the order specified. For each instruction, Dockerchecks whether it can reuse the instruction from the build cache.

Understanding how the build cache works, and how cache invalidation occurs,is critical for ensuring faster builds.For more information about the Docker build cache and how to optimize your builds,seeDocker build cache.

Pin base image versions

Image tags are mutable, meaning a publisher can update a tag to point to a newimage. This is useful because it lets publishers update tags to point tonewer versions of an image. And as an image consumer, it means youautomatically get the new version when you re-build your image.

For example, if you specify FROM alpine:3.19 in your Dockerfile, 3.19resolves to the latest patch version for 3.19.

# syntax=docker/dockerfile:1FROM alpine:3.19

At one point in time, the 3.19 tag might point to version 3.19.1 of theimage. If you rebuild the image 3 months later, the same tag might point to adifferent version, such as 3.19.4. This publishing workflow is best practice,and most publishers use this tagging strategy, but it isn't enforced.

The downside with this is that you're not guaranteed to get the same for everybuild. This could result in breaking changes, and it means you also don't havean audit trail of the exact image versions that you're using.

To fully secure your supply chain integrity, you can pin the image version to aspecific digest. By pinning your images to a digest, you're guaranteed toalways use the same image version, even if a publisher replaces the tag with anew image. For example, the following Dockerfile pins the Alpine image to thesame tag as earlier, 3.19, but this time with a digest reference as well.

# syntax=docker/dockerfile:1FROM alpine:3.19@sha256:13b7e62e8df80264dbb747995705a986aa530415763a6c58f84a3ca8af9a5bcd

With this Dockerfile, even if the publisher updates the 3.19 tag, your buildswould still use the pinned image version:13b7e62e8df80264dbb747995705a986aa530415763a6c58f84a3ca8af9a5bcd.

While this helps you avoid unexpected changes, it's also more tedious to haveto look up and include the image digest for base image versions manually eachtime you want to update it. And you're opting out of automated security fixes,which is likely something you want to get.

Docker Scout has a built-inOutdated base imagespolicy that checks forwhether the base image version you're using is in fact the latest version. Thispolicy also checks if pinned digests in your Dockerfile correspond to thecorrect version. If a publisher updates an image that you've pinned, the policyevaluation returns a non-compliant status, indicating that you should updateyour image.

Docker Scout also supports an automated remediation workflow for keeping yourbase images up-to-date. When a new image digest is available, Docker Scout canautomatically raise a pull request on your repository to update yourDockerfiles to use the latest version. This is better than using a tag thatchanges the version automatically, because you're in control and you have anaudit trail of when and how the change occurred.

For more information about automatically updating your base images with DockerScout, seeRemediation

When you check in a change to source control or create a pull request, useGitHub Actions or another CI/CD pipeline toautomatically build and tag a Docker image and test it.

Dockerfile instructions

Follow these recommendations on how to properly use theDockerfile instructionsto create an efficient and maintainable Dockerfile.

FROM

Whenever possible, use current official images as the basis for yourimages. Docker recommends theAlpine image as itis tightly controlled and small in size (currently under 6 MB), while stillbeing a full Linux distribution.

For more information about the FROM instruction, seeDockerfile reference for the FROM instruction.

LABEL

You can add labels to your image to help organize images by project, recordlicensing information, to aid in automation, or for other reasons. For eachlabel, add a line beginning with LABEL with one or more key-value pairs.The following examples show the different acceptable formats. Explanatory comments are included inline.

Strings with spaces must be quoted or the spaces must be escaped. Innerquote characters ("), must also be escaped. For example:

# Set one or more individual labelsLABEL com.example.version="0.0.1-beta"LABEL vendor1="ACME Incorporated"LABEL vendor2=ZENITH\ IncorporatedLABEL com.example.release-date="2015-02-12"LABEL com.example.version.is-production=""

An image can have more than one label. Prior to Docker 1.10, it was recommendedto combine all labels into a single LABEL instruction, to prevent extra layersfrom being created. This is no longer necessary, but combining labels is stillsupported. For example:

# Set multiple labels on one lineLABEL com.example.version="0.0.1-beta" com.example.release-date="2015-02-12"

The above example can also be written as:

# Set multiple labels at once, using line-continuation characters to break long linesLABEL 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"

SeeUnderstanding object labelsfor guidelines about acceptable label keys and values. For information aboutquerying labels, refer to the items related to filtering inManaging labels on objects.See alsoLABEL in the Dockerfile reference.

RUN

Split long or complex RUN statements on multiple lines separated withbackslashes to make your Dockerfile more readable, understandable, andmaintainable.

For more information about RUN, seeDockerfile reference for the RUN instruction.

apt-get

Probably the most common use case for RUN is an application of apt-get.Because it installs packages, the RUN apt-get command has several counter-intuitive behaviors tolook out for.

Always combine RUN apt-get update with apt-get install in the same RUNstatement. For example:

RUN apt-get update && apt-get install -y \ package-bar \ package-baz \ package-foo \ && rm -rf /var/lib/apt/lists/*

Using apt-get update alone in a RUN statement causes caching issues andsubsequent apt-get install instructions to fail. For example, this issue will occur in the following Dockerfile:

# syntax=docker/dockerfile:1FROM ubuntu:22.04RUN apt-get updateRUN apt-get install -y curl

After building the image, all layers are in the Docker cache. Suppose you latermodify apt-get install by adding an extra package as shown in the following Dockerfile:

# syntax=docker/dockerfile:1FROM ubuntu:22.04RUN apt-get updateRUN 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 isn't executedbecause the build uses the cached version. Because the apt-get update isn'trun, your build can potentially get an outdated version of the curl andnginx 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-getrecommendations.

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/*

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

In addition, when you clean up the apt cache by removing /var/lib/apt/lists itreduces the image size, since the apt cache isn't stored in a layer. Since theRUN statement starts with apt-get update, the package cache is alwaysrefreshed prior to apt-get install.

Official Debian and Ubuntu imagesautomatically run apt-get clean, so explicit invocation is not required.

Using pipes

Some 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 onlyevaluates 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 longas the wc -l command succeeds, even if the wget command fails.

If you want the command to fail due to an error at any stage in the pipe,prepend set -o pipefail && to ensure that an unexpected error prevents thebuild from inadvertently succeeding. For example:

RUN set -o pipefail && wget -O - https://some.site | wc -l > /number

Note

Not all shells support the -o pipefail option.

In cases such as the dash shell onDebian-based images, consider using the exec form of RUN to explicitlychoose a shell that does support the pipefail option. For example:

RUN ["/bin/bash", "-c", "set -o pipefail && wget -O - https://some.site | wc -l > /number"]

CMD

The CMD instruction should be used to run the software contained in yourimage, along with any arguments. CMD should almost always be used in the formof CMD ["executable", "param1", "param2"]. Thus, if the image is for aservice, such as Apache and Rails, you would run something like CMD ["apache2","-DFOREGROUND"]. Indeed, this form of the instruction is recommendedfor 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 likedocker 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 withENTRYPOINT, unlessyou and your expected users are already quite familiar with how ENTRYPOINTworks.

For more information about CMD, seeDockerfile reference for the CMD instruction.

EXPOSE

The EXPOSE instruction indicates the ports on which a container listensfor 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 (for example, MYSQL_PORT_3306_TCP).

For more information about EXPOSE, seeDockerfile reference for the EXPOSE instruction.

ENV

To make new software easier to run, you can use ENV to update thePATH environment variable for the software your container installs. Forexample, ENV PATH=/usr/local/nginx/bin:$PATH ensures that CMD ["nginx"]just works.

The ENV instruction is also useful for providing the required environmentvariables specific to services you want to containerize, such as Postgres’sPGDATA.

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.3ENV PG_VERSION=9.3.4RUN curl -SL https://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgres &&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 toautomatically bump the version of the software in your container.

Each ENV line creates a new intermediate layer, just like RUN commands. Thismeans that even if you unset the environment variable in a future layer, itstill persists in this layer and its value can be dumped. You can test this bycreating a Dockerfile like the following, and then building it.

# syntax=docker/dockerfile:1FROM alpineENV ADMIN_USER="mark"RUN echo $ADMIN_USER > ./markRUN unset ADMIN_USER
$ docker run --rm test sh -c 'echo $ADMIN_USER'mark

To prevent this, and really unset the environment variable, use a RUN commandwith shell commands, to set, use, and unset the variable all in a single layer.You can separate your commands with ; or &&. If you use the second method,and one of the commands fails, the docker build also fails. This is usually agood idea. Using \ as a line continuation character for Linux Dockerfilesimproves readability. You could also put all of the commands into a shell scriptand have the RUN command just run that shell script.

# syntax=docker/dockerfile:1FROM alpineRUN export ADMIN_USER="mark" \ && echo $ADMIN_USER > ./mark \ && unset ADMIN_USERCMD sh
$ docker run --rm test sh -c 'echo $ADMIN_USER'

For more information about ENV, seeDockerfile reference for the ENV instruction.

ADD or COPY

ADD and COPY are functionally similar. COPY supports basic copying offiles into the container, from thebuild contextor from a stage in amulti-stage build.ADD supports features for fetching files from remote HTTPS and Git URLs, andextracting tar files automatically when adding files from the build context.

You'll mostly want to use COPY for copying files from one stage to another ina multi-stage build. If you need to add files from the build context to thecontainer temporarily to execute a RUN instruction, you can often substitutethe COPY instruction with a bind mount instead. For example, to temporarilyadd a requirements.txt file for a RUN pip install instruction:

RUN --mount=type=bind,source=requirements.txt,target=/tmp/requirements.txt \ pip install --requirement /tmp/requirements.txt

Bind mounts are more efficient than COPY for including files from the buildcontext in the container. Note that bind-mounted files are only addedtemporarily for a single RUN instruction, and don't persist in the finalimage. If you need to include files from the build context in the final image,use COPY.

The ADD instruction is best for when you need to download a remote artifactas part of your build. ADD is better than manually adding files usingsomething like wget and tar, because it ensures a more precise build cache.ADD also has built-in support for checksum validation of the remoteresources, and a protocol for parsing branches, tags, and subdirectories fromGit URLs.

The following example uses ADD to download a .NET installer. Combined withmulti-stage builds, only the .NET runtime remains in the final stage, nointermediate files.

# syntax=docker/dockerfile:1FROM scratch AS srcARG DOTNET_VERSION=8.0.0-preview.6.23329.7ADD --checksum=sha256:270d731bd08040c6a3228115de1f74b91cf441c584139ff8f8f6503447cebdbb \ https://dotnetcli.azureedge.net/dotnet/Runtime/$DOTNET_VERSION/dotnet-runtime-$DOTNET_VERSION-linux-arm64.tar.gz /dotnet.tar.gzFROM mcr.microsoft.com/dotnet/runtime-deps:8.0.0-preview.6-bookworm-slim-arm64v8 AS installer# Retrieve .NET RuntimeRUN --mount=from=src,target=/src <<EOFmkdir -p /dotnettar -oxzf /src/dotnet.tar.gz -C /dotnetEOFFROM mcr.microsoft.com/dotnet/runtime-deps:8.0.0-preview.6-bookworm-slim-arm64v8COPY --from=installer /dotnet /usr/share/dotnetRUN ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet

For more information about ADD or COPY, see the following:

  • Dockerfile reference for the ADD instruction
  • Dockerfile reference for the COPY instruction

ENTRYPOINT

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.

The following is an example of an image for the command line tool s3cmd:

ENTRYPOINT ["s3cmd"]CMD ["--help"]

You can use the following command to run the image and show the command's help:

$ docker run s3cmd

Or, you can use the right parameters to execute a command, like in the following example:

$ 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, thePostgres Official Imageuses the following script as its ENTRYPOINT:

#!/bin/bashset -eif [ "$1" = 'postgres' ]; then chown -R postgres "$PGDATA" if [ -z "$(ls -A "$PGDATA")" ]; then gosu postgres initdb fi exec gosu postgres "$@"fiexec "$@"

This script usesthe exec Bash 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. For more information, see theENTRYPOINT reference.

In the following example, a helper script is copied into the container and run via ENTRYPOINT oncontainer start:

COPY ./docker-entrypoint.sh /ENTRYPOINT ["/docker-entrypoint.sh"]CMD ["postgres"]

This script lets you interact with Postgres in several ways.

It can simply start Postgres:

$ docker run postgres

Or, you can use it to run Postgres and pass parameters to the server:

$ docker run postgres postgres --help

Lastly, you can use it to start a totally different tool, such as Bash:

$ docker run --rm -it postgres bash

For more information about ENTRYPOINT, seeDockerfile reference for the ENTRYPOINT instruction.

VOLUME

You should use the VOLUME instruction to expose any database storage area,configuration storage, or files and folders created by your Docker container. Youare strongly encouraged to use VOLUME for any combination of mutable or user-serviceableparts of your image.

For more information about VOLUME, seeDockerfile reference for the VOLUME instruction.

USER

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 the following example:

RUN groupadd -r postgres && useradd --no-log-init -r -g postgres postgres

Note

Consider an explicit UID/GID.

Users and groups in an image are assigned a non-deterministic UID/GID in thatthe "next" UID/GID is assigned regardless of image rebuilds. So, if it’scritical, you should assign an explicit UID/GID.

Note

Due to anunresolved bug in theGo archive/tar package's handling of sparse files, attempting to create a userwith a significantly large UID inside a Docker container can lead to diskexhaustion because /var/log/faillog in the container layer is filled withNULL (\0) characters. A workaround is to pass the --no-log-init flag touseradd. The Debian/Ubuntu adduser wrapper does not support this flag.

Avoid installing or using sudo as it has unpredictable TTY andsignal-forwarding behavior that can cause problems. If you absolutely needfunctionality similar to sudo, such as initializing the daemon as root butrunning it as non-root, consider using“gosu”.

Lastly, to reduce layers and complexity, avoid switching USER back and forthfrequently.

For more information about USER, seeDockerfile reference for the USER instruction.

WORKDIR

For clarity and reliability, you should always use absolute paths for yourWORKDIR. Also, you should use WORKDIR instead of proliferating instructionslike RUN cd … && do-something, which are hard to read, troubleshoot, andmaintain.

For more information about WORKDIR, seeDockerfile reference for the WORKDIR instruction.

ONBUILD

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 that the parent Dockerfile givesto the child Dockerfile.

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

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 theDockerfile, as you can see inRuby’s ONBUILD variants.

Images built with 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 imagefails catastrophically if the new build's context is missing the resource beingadded. Adding a separate tag, as recommended above, helps mitigate this byallowing the Dockerfile author to make a choice.

For more information about ONBUILD, seeDockerfile reference for the ONBUILD instruction.

Building best practices (2024)

References

Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5702

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.