Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Build smaller container images using S2I

February 15, 2023
Lumír Balhar
Related topics:
ContainersLinux
Related products:
Red Hat Enterprise Linux

Share:

    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

    Related Posts

    • How to pick the right container base image

    • Building .NET Core container images using S2I

    • Flexible Images or Using S2I for Image Configuration

    • Getting started with OpenShift Java S2I

    Recent Posts

    • Container starting and termination order in a pod

    • More Essential AI tutorials for Node.js Developers

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    What’s up next?

    Featured image for Red Hat Universal Base Image

    Read the Red Hat Universal Base Images e-book to discover why choosing a base image is strategically important for building cloud-native apps.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue