Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Minimizing Container Image Size


Recommendation

Description

Choose a small base image

Base images are container images that do not have a parent. Container images usually have their own root filesystem with

an operating system installed. Typically, you will create a new image using a base image that contains an operating system. Consider the following when selecting a base image:

  • Operating system distribution: Determine if your application requires specific libraries or tools from a specific operating system
  • Base image size: Use small base images, such as busybox (2 MB) or alpine (5 MB). In contrast, a standard minimal operating system, such as Fedora or CentOS could be up to 200MB in size.
  • Updates: Use only base images from "official repositories", such as Docker Hub.

Be mindful of where your Dockerfile is located (i.e. Understand the build context)

Irrespective of where the Dockerfile is located, all 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 an image results in a larger build context and larger image size.

Use multi-stage builds

Use multi-stage builds, to only copy the artifacts you need into the final image.

Tools and debug information can be added to intermediate build stages without increasing the size of the final image.

Don’t install unnecessary packages

Avoid installing extra or unnecessary packages. This will reduce complexity, dependencies,

file sizes, and

build times and image sizes.

For example, don't include a text editor in a database image.

Minimize the number of layers

RUN, COPY, ADD instructions create layers and increase the size of image.

To reduce the number of layers and the image size, don't use ADD to download packages from URLs. Use curl or wget and delete the files you no longer need after they’ve been extracted.

Example:

RUN mkdir -p /usr/src/ether \
    && curl -SL http://vacuum.com/huge.tar.xz \
    | tar -xJC /usr/src/ether \
    && make -C /usr/src/ether all