Building Container Images: Difference between revisions

From Leo's Notes
This page was last edited on 8 November 2021, at 05:35.
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..."
 
No edit summary
Line 1: Line 1:
<br />
<br />


== Tips ==
==Tips==
Review the Dockerfile best practices at https://docs.docker.com/develop/develop-images/dockerfile_best-practices/


=== RUN commands ===
===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 <code>&&</code> or <code>;</code> (with <code>set-x,</code> which stops the script when a non zero exit code is returned).
Try to bundle multiple RUN commands together to reduce the number of container image layers and their overall size. Concatenating multiple commands with <code>&&</code> or <code>;</code> (with <code>set-x,</code> which stops the script when a non zero exit code is returned).
{{Highlight
{{Highlight
Line 14: Line 15:
| lang = text
| lang = text
}}
}}
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 ===
Remember to clean up at the end of each RUN to minimize the layer sizes. Things you can do include:
Yum or dnf package managers are used on Red Hat based distros.
* <code>rm -rf /tmp/* /var/cache/* /var/tmp/*</code>
* Clean up package manager caches and remove unnecessary build-only packages


* Install packages with <code>yum -y install</code> to avoid prompts
Better yet, use multi-staged builds (https://docs.docker.com/develop/develop-images/multistage-build/) to bundle the results of multiple layers into one.
* <code>yum -y clean all</code>, and clean up caches <code>rm -rf /var/cache</code>
* Apt-based package managers


{{highlight|lang=text|code=
FROM centos:8 as build
RUN yum -y update
RUN yum -y install vim
FROM centos:8
## We can copy specific artifacts from the build container. Eg:
COPY --from=build /usr/bin/vim /usr/bin/vim
}}
===Yum/dnf-based package managers===
Yum or dnf package managers are used on Red Hat based distros.
*Install packages with <code>yum -y install</code> to avoid prompts
*At the end of the run, <code>yum -y clean all</code>, and clean up caches <code>rm -rf /var/cache</code> afterwards
===Apt-based package managers===
Apt-based package managers are used on Ubuntu and Debian based distros.
Apt-based package managers are used on Ubuntu and Debian based distros.


* Set <code>DEBIAN_FRONTEND=noninteractive</code> to prevent some packages from prompting interactive input (<code>tzdata</code> for example), which leads to indefinite waiting for an user input
* Set <code>DEBIAN_FRONTEND=noninteractive</code> to prevent some packages from prompting interactive input (<code>tzdata</code> for example), which leads to indefinite waiting for an user input
* <code>rm -rf /var/lib/apt/lists/*</code> afterwards
*At the end of the run, <code>rm -rf /var/lib/apt/lists/*</code> to clean up.
 
===apk-based package managers ===
Alpine Linux uses the apk package manager. One neat feature with apk is the ability to group packages into virtual groups which we can uninstall with one command.


<br />
* Create virtual groups for build dependencies using <code>apk add --virtual=build-deps make gcc</code>. These groups can later be removed with <code>apk del --purge build-deps</code>.
* At the end of the run, <code>/var/cache/apk/*</code> to clean up.

Revision as of 05:35, 8 November 2021


Tips

Review the Dockerfile best practices at https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

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

Remember to clean up at the end of each RUN to minimize the layer sizes. Things you can do include:

  • rm -rf /tmp/* /var/cache/* /var/tmp/*
  • Clean up package manager caches and remove unnecessary build-only packages

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

FROM centos:8 as build
RUN yum -y update
RUN yum -y install vim

FROM centos:8
## We can copy specific artifacts from the build container. Eg:
COPY --from=build /usr/bin/vim /usr/bin/vim


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
  • At the end of the run, yum -y clean all, and clean up caches rm -rf /var/cache afterwards

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
  • At the end of the run, rm -rf /var/lib/apt/lists/* to clean up.

apk-based package managers

Alpine Linux uses the apk package manager. One neat feature with apk is the ability to group packages into virtual groups which we can uninstall with one command.

  • Create virtual groups for build dependencies using apk add --virtual=build-deps make gcc. These groups can later be removed with apk del --purge build-deps.
  • At the end of the run, /var/cache/apk/* to clean up.