After we launched Red Hat Hardened Images in May, I decided to migrate a containerized application I use at home to one of our images. I'd spent some time helping build the initial examples, and seen some issues with the distroless approach for a sample Python application, but I still needed to go deeper.
Knowing that minimization and distroless approaches shift how we need to build containerized applications, I decided to target the runtime environment with the smallest starting footprint. Our core-runtime is designed with dynamically linked applications in mind. No extra development tools, just libraries and a few other packages.
The goal: Take a containerized application I use somewhat regularly and rebuild it on a Red Hat Hardened Image. I have a functioning comparison point to know what done looks like.
The app: Pi-hole is a DNS sink I use to block access to sites on my home network. It also isn't a simple "run Ruby app on Ruby image" conversion, either. It's a mix of shell, compiled C, and JavaScript. There's some helper scripts in the project to make the containerized environment provide similar functionality as the full OS the Pi-hole expects to see. I can't simply run curl | bash on the installer and expect anything to work.
The method: Examine the upstream project container for a basic design but target, at minimum, a final image based on core-runtime with Bash added. I don't want to just change all the commands in the official Containerfile from Alpine, but actually examine what that project did, and why.
If I'm going to rebuild a perfectly working container image, I may as well make it a challenge. It really is still a work in progress, but I have a fully rebuilt image that I'm happy with. Here's what I learned along the way.
Multi-stage builds are important
The application is really 3 separate projects that get installed together. The compiled FTL DNS server is installed from a Github release, but the main Pi-hole application and the dashboard are cloned repos that get "installed". I don't need all the Git history or the supporting tooling, just the application files. The project installer also helpfully documents all the required packages, so I can review those against available RPMs.
Separating the build time artifacts and tools and run time tools to get that minimized footprint really benefits from multi-stage container builds. I keep all of the tracking, and install for the project in the builder stage, then just add the final files and the dependencies (like Bash) to the final stage.
RUN with mount
Even with the list published by the project, getting the final image right still took a little experimentation. Because these dependencies required RPMs installed in the final image, there are 2 basic options. There's no dnf in the core-runtime, so that's not an option.
The builder stage uses the core-runtime:builder image, which does include dnf for package installs. The first option is to install the needed RPMs to a specific location in the builder stage, then copy that location in the final stage into the target image.
That's a pretty common pattern and works fine. Passing installroot to dnf means you have a nice clean spot to land your packages for copying. However, because the actual dnf transaction happens in the build stage, the rpmdb in the final image gets overwritten by these new packages. This works, but not in the way you might expect.
With a "clean" install root, dnf treats this as a completely new transaction and resolves all dependencies for the package list. Because there is no pre-existing rpmdb, there are duplicated packages in the new root like glibc or filesystem in the transaction. There may be packages with higher capabilities installed, like installing coreutils where coreutils-single in the base image would have sufficed. When the COPY happens, this essentially lays down a whole new root filesystem over the existing image, leaving the original files on a lower layer. CVE scanners and SBOM generators that operate on the rpmdb still work, but what they see may be very different from a scan of the core-runtime base.
This installroot pattern is more suited to scratch builds than using a minimal base image.
The other option still uses dnf in the core-runtime:builder image to install the RPMs, just in a slightly different way than you may have seen before. It was certainly new to me.
The RUN directive in a Containerfile has the --mount option to attach a filesystem to this particular operation. You may have seen this as a way to pass a secret into a container build from a command-line argument as --mount type=secret which shows up in the image under /run/secrets. Here, we can use this to mount the core-runtime:builder image as a file system at a specific location, then use tools relative to that path.
RUN --mount=type=bind,from=registry.access.redhat.com/hi/core-runtime:latest-builder,target=/builder \
LD_LIBRARY_PATH=/builder/lib64:/builder/usr/lib64 \
RPM_CONFIGDIR=/builder/usr/lib/rpm \
/builder/usr/bin/dnf install -yThe whole image gets mounted at /builder and we run dnf right from there. As it does run from that mounted path, we also need to tell dnf where to look for the correct libraries and configuration files. Now, you can see there's more setup needed than just doing this in the build stage. But the result is the packages get installed, and the rpmdb is updated in the final target. When I use grype to look for CVEs, any of the Pi-hole dependencies I added manually get matched to the published feed correctly.
Conversion changes
I mentioned that the project publishes a list of dependencies for its installer, but that installer is also designed to work for a number of different operating systems. The container image is built on Alpine. That project has some helper scripts that get used to do things during build that would have been done by the installer.
The goal was to carry as few upstream project modifications as possible, but those helper scripts were fair game. The minimization goal means only add the absolute necessities, and that means looking at things like how updates for block lists are scheduled.
One packaging difference came up that I addressed a few different ways. The Busybox package shipped in Alpine creates all of the symlinks at package build, ours doesn't. These symlinks let the appropriate busybox function be called from the standard Linux utility location. At first I was hesitant to create the symlinks, and changed things to explicit calls. It made things clear but also meant maintaining busybox sed edits in a few files.
After a few rounds of builds and troubleshooting and an FTL update that showed some previously silent failures, I changed tack and created all the links at build time for the final stage. This was a simple single line in the Containerfile, and cleaned up a lot of minor downstream changes.
Busybox sidecar
The image was using Alpine's busybox package, which we have available to us as well. There are some differences in how they are packaged. The coreutils packages are also different in each distro. While looking at this conversion process, I was also testing out the published AI skills. Figuring out what was provided and how proved to be a little tedious, even with the help of an agent. And the choices the agent made weren't always the best.
So I made myself a little "sidecar" container, built from the core-runtime:builder with Busybox installed. That's all, nothing complicated. However, this meant I had an easy way to check for available commands with busybox --list, or the contents of a package with rpm -ql, or even simulate a full install with dnf --assumeno install and see all the packages that would get pulled in.
$ podman run --rm \
-it localhost/hum-cap:latest dnf install --assumeno --no-docs vim-minimal
Updating and loading repositories:
Repositories loaded.
Package Arch Version Repository Size
Installing:
vim-minimal x86_64 2:9.2.843-1.hum1 public-hummingbird-x86_64-rpms 1.8 MiB
Installing dependencies:
vim-data noarch 2:9.2.843-1.hum1 public-hummingbird-x86_64-rpms 10.2 KiB
Transaction Summary:
Installing: 2 packages
Total size of inbound packages is 924 KiB. Need to download 924 KiB.
After this operation, 2 MiB extra will be used (install 2 MiB, remove 0 B).
Operation aborted by the user.Integrating updates
With a working image, the next thing that I needed to work out was how to keep everything up to date. I had to track the project for updates and the images I was using. Red Hat Hardened Images are rebuilt whenever a change is detected in the images packages. I was going to have to automate all of this to make sure everything stayed together.
Enter renovatebot
Renovate is a pretty capable tool for keeping track of dependencies of various types. For Github, it can even create pull requests with the changes for review. Quite literally up first was the project releases. Every time there's a new release, I want to make sure to pull it into the build system. Github releases are an existing datasource, so I just needed to alter the Containerfile to give renovate something to track and modify. The versions became args with a helper comment for renovate to hook on to. Whenever Pi-hole pushes out new versions of anything, I get a candidate PR with the change to pull in that new version.
With the build stage done, the target stage was up next. First was the core-runtime image itself. No use having a rapidly rebuilt image if you aren't tracking those changes. Again, container images are already something renovate knows all about, I just needed to make sure it had the redhat version hints in the config and it was tracking our base.
In many cases, that may be all you need to monitor for your final image. But I had added RPMs during the final stage, so I wanted to make sure to track those too. Because Busybox isn't in the core-runtime image, it could be updated on a different cycle. Tracking those individually means PRs are opened for any updates that affect the final running image. The RPM datasource made it just as easy to add as the Github releases.
Testing testing
Finally, all of these automated possible changes and iterations meant adding testing to the builds. None of this is worth the time spent if the DNS sink doesn't actually sink the request. That meant testing whether the Pi-hole image did its job, but also that the unbound DNS server I was using from Red Hat Hardened Images did its job, and that they worked together as a pod like they would in the final deployment.
The result
That might sound like a whole lot of work, so what did I gain at the end? A regularly updated application, published as a pipeline, that's up to date and easy to understand. I've only had to pay attention to 1 update where an upstream project made a functional change in response to a CVE.
To do this for your app, head to images.redhat.com and find your own starting point.