Featured image for "Red Hat CodeReady Containers 1.31.2 makes the leap."

The Source-to-Image (S2I) framework makes it easy for developers to transfer their projects into ready-to-use and reproducible container images. S2I consists of a tool that prepares your container, along with many base images to choose from. S2I base container images are ready for multiple database engines like PostgreSQL or MariaDB as well as for programming language ecosystems like Python, PHP, Node.js, and more.

The need for a minimal container image

The biggest advantage of the S2I base container images is their universality. The base images contain a carefully selected set of pre-installed RPM packages that make it easy to build applications and their dependencies from source. The images also contain scripts that can handle the whole process of preparation of the final container image.

However, this versatility—their biggest advantage—is also their biggest flaw. The size of the base images has grown as we tried to support the majority of use cases reported by developers. The result is that the base container images might be too big for your use case and likely contain some tools and libraries that are not relevant for your application. This is especially problematic given the current popularity of microservices. And so, we have decided to start a new range of minimal container images addressing this market.

Use cases

What does this mean? What is the difference? Let's take a look, for example, at the S2I Python container image with Python 3.9 based on Red Hat Enterprise Linux (RHEL) 8. We started by switching from the UBI (the Universal Base Image based on RHEL) to the UBI-minimal base layer. The main difference here is that UBI-minimal has microdnf (written in C) instead of dnf (written in Python) as the main package manager. This change alone saves us half of the original size (75.5 MB → 37.6 MB).

The rest of the saved disk space comes from the minimized set of packages we pre-install into the minimal Python container image. The final result is that we have managed to shrink the total image size from 891 MB to 201 MB—a full 77% reduction in the size of the full container image.

The situation is similar for the Node.js 16 image, also based on RHEL 8. The minimal container image has only 188 MB instead of the full container image with 638 MB—more than 70 % saved.

Are there any disadvantages of the minimal container images? In general, no. If your project and all its dependencies are all in interpreted languages, the minimal container images will work for you the same way as the original does. A problem might appear if you need to compile some parts of your project or if any of your dependencies are not provided in pre-compiled packages (like wheels for Python). In that case, you can either use the full image, or you'll need to install devel libraries and header files into the minimal image. That requires some manual work, but do not be afraid; it is rather straightforward.

2 ways to use the minimal image

There are two possible ways to get the benefits of a minimal container image even when you need extra devel libraries and header files. The first one is to create a custom Dockerfile and build a custom container image on top of the minimal container image—in this case, you can still use all the universal scripts from the minimal container image. This approach is good if you need something special for both buildtime and runtime, because the installed packages stay in the image.

The second option is to build the application and its dependencies on the full container image and use the minimal one only for runtime. This approach is great if you need something special only during building but not at runtime. This way, all the pre-installed packages from the full container image are available when you need them.

Let's say we need to install python3-devel and gcc to be able to compile the uwsgi package from source. An example of the custom Dockerfile might look like this.

FROM python-39-minimal

# Add application sources to a directory that the assemble script expects them
# and set permissions so that the container runs without root access
USER 0
ADD app-src /tmp/src
RUN /usr/bin/fix-permissions /tmp/src

# Install packages necessary for compiling uwsgi from source
RUN microdnf install -y gcc python39-devel

USER 1001

# Install the dependencies
RUN /usr/libexec/s2i/assemble

# Set the default command for the resulting image
CMD /usr/libexec/s2i/run

In the next example, we use the full container image and all its pre-installed capabilities to build the app's dependencies, and then we move the entire prepared virtual environment to the minimal container image and additionally install httpd, which is a runtime dependency for our application.

# Part 1 - build

FROM python-39 as builder

# Add application sources to a directory that the assemble script expects them
# and set permissions so that the container runs without root access
USER 0
ADD app-src /tmp/src
RUN /usr/bin/fix-permissions /tmp/src
USER 1001

# Install the application's dependencies from PyPI
RUN /usr/libexec/s2i/assemble


# Part 2 - deploy

FROM python-39-minimal

# Copy app sources together with the whole virtual environment from the builder image
COPY --from=builder $APP\_ROOT $APP\_ROOT

# Install httpd package - runtime dependency of our application
USER 0
RUN microdnf install -y httpd
USER 1001

# Set the default command for the resulting image
CMD /usr/libexec/s2i/run

Conclusion

If you have any questions, want to discuss something with us, or just want to report that the minimal container images work for you, please feel free to open an issue in one of the relevant GitHub repositories mentioned above. We'll be happy to help you and hear about your use cases.

 

Last updated: August 14, 2023