Skip to main content
AI
  • Get started with AI

    • Red Hat AI

      Accelerate the development and deployment of enterprise AI solutions.

    • AI learning hub

      Explore learning materials and tools, organized by task.

    • AI interactive demos

      Click through scenarios with Red Hat AI, including training LLMs and more.

    • AI/ML learning paths

      Expand your Red Hat OpenShift AI knowledge using these learning resources.

    • AI quickstarts

      Focused AI use cases designed for fast deployment on Red Hat AI platforms.

    • No-cost AI training

      Foundational Red Hat AI training.

  • Featured resources

    • Red Hat OpenShift AI
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

Red Hat AI Factory with NVIDIA

Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.

Learn
  • Self-guided

    • Documentation

      Find answers, get step-by-step guidance, and learn how to use Red Hat products.

    • Learning paths

      Explore curated walkthroughs for common development tasks.

    • Guided learning

      Receive custom learning paths powered by our AI assistant.

    • See all learning
  • Hands-on

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

    • Interactive labs

      Learn by doing in these hands-on, browser-based experiences.

    • Interactive demos

      Click through product features in these guided tours.

  • Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics
  • Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
Build
  • Get started

    • Red Hat build of Podman Desktop

      A downloadable, local development hub to experiment with our products and builds.

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

  • Download products

    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products
  • Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset
  • References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
Community
  • Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions
  • Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github
  • Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

Join the Red Hat Developer program

Download Red Hat products and project builds, access support documentation, learning content, and more.

ConsoleDocsSupport Search
New For you

Recommended

We'll recommend resources you may like as you browse. Try these suggestions for now.

  • Product downloads
  • Cheat sheets
  • Red Hat Developer learning portal
  • Developer blog
  • E-books
  • Developer Sandbox
Log in

Get more with a Red Hat Developer account

  • Product downloads
  • Console access
  • Event registration
  • Training & trials
Explore more account benefitsLog in or register

A subscription may be required for some services.

Contact us

Build smaller container images using S2I

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

    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

    • A day in the life of an NVIDIA H100 on Red Hat OpenShift 5

    • Migrate BuildConfigs to builds for Red Hat OpenShift with one plug-in install

    • Run Red Hat Enterprise Linux CoreOS on NVIDIA BlueField DPUs

    • Agent harness engineering: Separating model capabilities from enforcement

    • What's new for developers in Red Hat Enterprise Linux 10.3 and 9.9

    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

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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