Building Container Images

From Leo's Notes
Revision as of 05:25, 8 November 2021 by Leo (talk | contribs) (Created page with "<br /> == Tips == === RUN commands === Try to bundle multiple RUN commands together to reduce the number of container image layers and their overall size. Concatenating mult...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
This page was last edited on 8 November 2021, at 05:25.


Tips

RUN commands

Try to bundle multiple RUN commands together to reduce the number of container image layers and their overall size. Concatenating multiple commands with && or ; (with set-x, which stops the script when a non zero exit code is returned).

RUN set -ex; \
   yum -y install vim; \
   yum clean all

## or,
RUN yum -y install vim && yum clean all

Alternatively, use multi-staged builds (https://docs.docker.com/develop/develop-images/multistage-build/) to bundle the results of multiple layers into one.

Yum/dnf-based package managers

Yum or dnf package managers are used on Red Hat based distros.

  • Install packages with yum -y install to avoid prompts
  • yum -y clean all, and clean up caches rm -rf /var/cache
  • Apt-based package managers

Apt-based package managers are used on Ubuntu and Debian based distros.

  • Set DEBIAN_FRONTEND=noninteractive to prevent some packages from prompting interactive input (tzdata for example), which leads to indefinite waiting for an user input
  • rm -rf /var/lib/apt/lists/* afterwards