Skip to content

Switch to UBI9 minimal multi-stage image#146

Open
SudipSinha wants to merge 5 commits into
mainfrom
build/ubi9-minimal
Open

Switch to UBI9 minimal multi-stage image#146
SudipSinha wants to merge 5 commits into
mainfrom
build/ubi9-minimal

Conversation

@SudipSinha
Copy link
Copy Markdown
Member

@SudipSinha SudipSinha commented May 7, 2026

Summary

  • Replace single-stage ubi9/python-312 with a two-stage build using ubi9/python-312-minimal as runtime
  • Builder stage compiles wheels; runtime has no compilers, dev headers, or uv binary
  • Reduces Trivy findings from ~3,000 to ~108 with zero scan workarounds
  • FIPS crypto policy and MariaDB connector support preserved
  • Bumps pip 26.0.1 → 26.1 (CVE-2026-6357)

Test plan

  • podman build -f Containerfile -t trustyai:test . succeeds
  • CI build job passes
  • Trivy scan shows reduced findings with no suppression flags

🤖 Generated with Claude Code

Summary by Sourcery

Switch the container build to a multi-stage UBI9 Python 3.12 minimal runtime image while preserving FIPS and MariaDB support and reducing base image vulnerabilities.

Enhancements:

  • Introduce a two-stage container build separating a full UBI9 Python builder from a minimal UBI9 Python runtime image.
  • Retain FIPS crypto policy configuration and MariaDB client support within the new minimal runtime image.
  • Update the base image metadata to reference the UBI9 Python 3.12 minimal runtime image and standardize the application work directory.

Build:

  • Refactor the Containerfile to use a multi-stage build with dependency installation in the builder stage and only runtime artifacts copied into the final image.
  • Switch to microdnf for minimal-image package installation and adjust MariaDB-related package installation accordingly.
  • Upgrade the runtime pip version to 26.1.1 to address base image CVEs and ensure a clean, cache-free install.

Summary by CodeRabbit

  • Refactor
    • Optimized container image build for improved efficiency and reduced image footprint.
    • Added conditional FIPS policy support for enhanced security compliance.
    • Introduced optional MariaDB support with conditional dependency installation.

Replace single-stage ubi9/python-312 with a two-stage build:
- Builder: ubi9/python-312 (full) compiles wheels via uv
- Runtime: ubi9/python-312-minimal (no compilers, no dev headers, no uv)

Reduces Trivy findings from ~3,000 to ~108 with zero scan
workarounds. FIPS crypto policy and MariaDB support preserved.

Also bumps pip 26.0.1 → 26.1 (CVE-2026-6357).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@SudipSinha SudipSinha added the ok-to-test Proceed with CI testing label May 7, 2026
@SudipSinha SudipSinha self-assigned this May 7, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 7, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

The Containerfile is refactored from a single-stage build into a two-stage build that separates dependency installation (builder) from runtime execution (minimal). The builder stage installs Python dependencies with optional MariaDB dev packages, while the runtime stage applies conditional FIPS policy, conditionally installs MariaDB shared libraries only, and copies built artifacts from the builder image.

Changes

Multi-Stage Container Build Refactor

Layer / File(s) Summary
Builder Stage Dependencies
Containerfile
New builder stage uses ubi9/python-312:latest to install Python dependencies via uv pip install with conditional MariaDB development libraries, then removes uv and build caches.
Runtime Stage Base and Conditional Setup
Containerfile
New minimal runtime stage uses ubi9/python-312-minimal:latest with conditional FIPS crypto policy installation and configuration when ENABLE_FIPS_POLICY=true, and conditionally installs only MariaDB runtime shared libraries.
Artifact Transfer and Pip Upgrade
Containerfile
Runtime stage copies built Python site-packages from multiple lib roots and console scripts from builder stage, upgrades pip, sets WORKDIR, and copies application source and dependency metadata files.
Runtime Environment and User Setup
Containerfile
Sets Python runtime environment variables, exports FIPS_POLICY_ENABLED flag, switches to non-root user 1001, and exposes ports 8080 and 4443.
OCI Metadata and Labels
Containerfile
Extends OCI labels with org.opencontainers.image.base.name referencing the minimal runtime image while preserving existing metadata labels.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Suggested labels

dependencies

Suggested reviewers

  • RobGeada

Poem

🐰 Two stages now dance where one did stand,
Building first, running lean across the land.
Builder gathers, runtime keeps only what's needed,
Dependencies parsed, FIPS policies heeded.
A minimal image, efficient and bright,
Two-stage containers burning containers-right! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: switching from a single-stage UBI9 Python image to a two-stage build using the minimal variant for runtime.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch build/ubi9-minimal

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 7, 2026

Reviewer's Guide

Converts the container build to a two-stage UBI9 image (full ubi9/python-312 builder + minimal ubi9/python-312-minimal runtime), compiling dependencies in the builder, slimming the runtime (no compilers/uv/dev headers), preserving FIPS and MariaDB support, and bumping pip to address a CVE while adding base-image metadata.

Flow diagram for conditional FIPS and MariaDB setup in runtime image

flowchart TD
    A[Build_arguments] --> B{ENABLE_FIPS_POLICY_equals_true?}

    B -- yes --> C[Install_crypto_policies_scripts_with_microdnf]
    C --> D[Run_update_crypto_policies_set_FIPS]
    D --> E[Clean_microdnf_and_remove_caches]

    B -- no --> F[Skip_FIPS_crypto_policy_setup]

    A --> G{EXTRAS_contains_mariadb?}

    G -- yes --> H[Download_and_verify_mariadb_repo_setup]
    H --> I[Run_mariadb_repo_setup_for_11_4]
    I --> J[Install_MariaDB_shared_11_4_with_microdnf]
    J --> K[Clean_microdnf_and_remove_caches]

    G -- no --> L[Skip_MariaDB_shared_install]

    E --> M[Runtime_image_ready_for_pip_upgrade_and_artifact_copy]
    F --> M
    K --> M
    L --> M
Loading

File-Level Changes

Change Details Files
Introduce multi-stage build using full UBI9 Python image as builder and UBI9 Python minimal image as runtime to slim the final image and reduce vulnerabilities.
  • Replace single-stage FROM ubi9/python-312 with Stage 1 builder FROM ubi9/python-312 and Stage 2 runtime FROM ubi9/python-312-minimal
  • Move Python dependency installation into the builder stage running as user 1001
  • Copy site-packages and bin artifacts from builder into the minimal runtime image
Containerfile
Refactor MariaDB dependency handling to build against full client in the builder and ship only shared libraries in the runtime.
  • In builder stage, conditionally install MariaDB-shared and MariaDB-devel 11.4.x via dnf when EXTRAS includes mariadb
  • In runtime stage, conditionally install only MariaDB-shared 11.4.x via microdnf when EXTRAS contains mariadb
  • Ensure MariaDB repo setup script is validated, cleaned up, and dnf/microdnf caches are removed
Containerfile
Adjust FIPS crypto policy configuration to work in the minimal runtime image while minimizing package footprint and caches.
  • Remove the verbose FIPS verification/status RUN block from the image build
  • In runtime stage, conditionally install crypto-policies-scripts with microdnf and run update-crypto-policies --set FIPS when ENABLE_FIPS_POLICY=true
  • Clean up microdnf caches and temporary data after FIPS configuration
Containerfile
Update pip and dependency install tooling to address a pip CVE and ensure uv is not present in the final image.
  • In builder stage, upgrade pip to 26.1.1 and install uv 0.11.1 before installing project extras via uv pip
  • Uninstall uv in the builder after dependency installation and clean caches
  • In runtime stage, only upgrade system pip to 26.1.1 and avoid installing uv
Containerfile
Tighten runtime image configuration and metadata to reflect the new minimal base and layout.
  • Set WORKDIR /opt/app-root explicitly in the runtime stage and copy application source, pyproject.toml, and README.md there
  • Preserve Python-related environment settings and FIPS_POLICY_ENABLED while running the container as user 1001 and exposing ports 8080 and 4443
  • Add org.opencontainers.image.base.name label pointing to ubi9/python-312-minimal in the image metadata
Containerfile

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

- Add script validation to runtime MariaDB step (parity with builder)
- Standardize on pip (not pip3) in both stages
- Bump pip 26.1 → 26.1.1

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@SudipSinha SudipSinha added security technical-debt Code that works but needs improvement (untested, deprecated patterns, etc.) labels May 7, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
Containerfile (1)

13-13: ⚡ Quick win

Pin base images by digest instead of :latest

At Line 13 and Line 43, using :latest makes builds non-reproducible and can silently change vulnerability posture between runs. Prefer immutable digests for both stages.

Also applies to: 43-43

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Containerfile` at line 13, Replace both mutable base image tags that
currently use :latest with immutable image digests: update the builder stage
"FROM registry.access.redhat.com/ubi9/python-312:latest AS builder" and the
final stage FROM line (the second use of :latest at line 43) to use the specific
sha256@<digest> for each image; fetch the correct digests from the registry (or
your image manifest) and substitute them so both FROM lines are pinned to their
respective digests to ensure reproducible, immutable builds.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Containerfile`:
- Around line 19-23: Replace the current weak checks around the downloaded
mariadb_repo_setup script with a cryptographic integrity check: after
downloading mariadb_repo_setup and before chmod/./mariadb_repo_setup, fetch a
trusted SHA256 checksum (hardcoded or from a verified source), compute the
downloaded file's SHA256 (e.g., via sha256sum) and compare it, and abort the
build if it doesn't match; apply the same SHA256 verification and abort logic to
the second Stage 2 block that also downloads and runs ./mariadb_repo_setup so
both occurrences (the mariadb_repo_setup download + ./mariadb_repo_setup
execution) only run after successful checksum validation.
- Around line 35-37: The Dockerfile RUN line installs the package using the
EXTRAS variable which expands to an invalid ".[]" when EXTRAS is empty; change
the pip install logic so it installs "." when EXTRAS is empty and installs
".[$EXTRAS]" only when EXTRAS is non-empty (i.e., add a shell conditional around
the pip install step that checks EXTRAS before choosing between "." and
".[$EXTRAS]"); update the RUN sequence that contains the pip install command
(the line with uv pip install --no-cache ".[$EXTRAS]") to use that conditional
while keeping the surrounding upgrade/uninstall steps intact.

---

Nitpick comments:
In `@Containerfile`:
- Line 13: Replace both mutable base image tags that currently use :latest with
immutable image digests: update the builder stage "FROM
registry.access.redhat.com/ubi9/python-312:latest AS builder" and the final
stage FROM line (the second use of :latest at line 43) to use the specific
sha256@<digest> for each image; fetch the correct digests from the registry (or
your image manifest) and substitute them so both FROM lines are pinned to their
respective digests to ensure reproducible, immutable builds.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ce4fbd44-380e-4fb0-b52c-2ad5851b307c

📥 Commits

Reviewing files that changed from the base of the PR and between ccc9091 and 8b4a481.

📒 Files selected for processing (1)
  • Containerfile

Comment thread Containerfile
Comment thread Containerfile
SudipSinha and others added 2 commits May 8, 2026 08:31
The ci-build workflow injects a quay.expires-after label after the
last FIPS documentation label using sed. Moving base.name before
it restores the expected line-ending pattern.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
GitHub Actions runners lack RHEL entitlement certificates, so no
rhel-* repos exist and microdnf treats the missing pattern as an
error. The UBI repos alone provide crypto-policies-scripts.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 8, 2026

PR image build and manifest generation completed successfully!

📦 PR image: quay.io/trustyai/trustyai-service-python-ci:08535f55ecca0ff77a8859e0fae7022c59818498

🗂️ CI manifests

devFlags:
  manifests:
    - contextDir: config
      sourcePath: ''
      uri: https://api.github.com/repos/trustyai-explainability/trustyai-service-operator-ci/tarball/service-python-08535f55ecca0ff77a8859e0fae7022c59818498

…repo

The builder stage inherits user 1001 from the base image, but dnf
install requires root. Add USER root before and USER 1001 after the
MariaDB block. Replace four architecture-specific --disablerepo flags
with a single glob (--disablerepo='rhel-*') that works on any arch
without warnings.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@SudipSinha SudipSinha marked this pull request as ready for review May 8, 2026 07:51
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Consider extracting the pip/uv versions into build ARGs used in both stages so they stay in sync without having to update multiple hard‑coded occurrences.
  • The MariaDB repository setup and installation logic is now duplicated (and slightly diverging) between builder and runtime; factoring this into a common pattern or clearly separating responsibilities would make it easier to keep behavior consistent over time.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider extracting the pip/uv versions into build ARGs used in both stages so they stay in sync without having to update multiple hard‑coded occurrences.
- The MariaDB repository setup and installation logic is now duplicated (and slightly diverging) between builder and runtime; factoring this into a common pattern or clearly separating responsibilities would make it easier to keep behavior consistent over time.

## Individual Comments

### Comment 1
<location path="Containerfile" line_range="20-29" />
<code_context>
-# For details, see: docs/FIPS_compliance.md
-# ===========================================================================================
+# Install MariaDB dev libraries if needed
+RUN if [[ "$EXTRAS" == *"mariadb"* ]]; then \
+        curl --fail -LsSO https://r.mariadb.com/downloads/mariadb_repo_setup && \
+        grep -q "mariadb_repo_setup" mariadb_repo_setup || { echo "ERROR: Downloaded script appears invalid"; exit 1; } && \
+        [ -s mariadb_repo_setup ] || { echo "ERROR: Downloaded script is empty"; exit 1; } && \
+        chmod +x mariadb_repo_setup && \
+        ./mariadb_repo_setup --mariadb-server-version="mariadb-11.4" --skip-check-installed && \
+        dnf install -y --disablerepo='rhel-*' \
+                       MariaDB-shared-11.4* MariaDB-devel-11.4* && \
+        dnf clean all && \
+        rm -rf /var/cache/dnf /var/cache/yum /root/.cache mariadb_repo_setup; \
+    fi
+
+USER 1001
</code_context>
<issue_to_address>
**🚨 suggestion (security):** MariaDB repo script download has only a basic integrity check; consider strengthening verification.

The current checks help detect corruption but not a malicious script served from the correct URL. Please consider verifying a published checksum/signature from MariaDB, or at least pinning a specific versioned URL, to better protect this root-run remote script path.

Suggested implementation:

```
ARG MARIADB_REPO_SETUP_VERSION="2024-05-24"
ARG MARIADB_REPO_SETUP_SHA256=""

# Install MariaDB dev libraries if needed
RUN if [[ "$EXTRAS" == *"mariadb"* ]]; then \
        REPO_SETUP_URL="https://r.mariadb.com/downloads/mariadb_repo_setup-${MARIADB_REPO_SETUP_VERSION}" && \
        curl --fail --show-error --location --proto '=https' --tlsv1.2 -o mariadb_repo_setup "${REPO_SETUP_URL}" && \
        if [[ -n "${MARIADB_REPO_SETUP_SHA256}" ]]; then \
            echo "${MARIADB_REPO_SETUP_SHA256}  mariadb_repo_setup" | sha256sum -c - || { echo "ERROR: mariadb_repo_setup checksum verification failed"; exit 1; }; \
        fi && \
        grep -q "mariadb_repo_setup" mariadb_repo_setup || { echo "ERROR: Downloaded script appears invalid"; exit 1; } && \
        [ -s mariadb_repo_setup ] || { echo "ERROR: Downloaded script is empty"; exit 1; } && \
        chmod +x mariadb_repo_setup && \
        ./mariadb_repo_setup --mariadb-server-version="mariadb-11.4" --skip-check-installed && \
        dnf install -y --disablerepo='rhel-*' \
                       MariaDB-shared-11.4* MariaDB-devel-11.4* && \
        dnf clean all && \
        rm -rf /var/cache/dnf /var/cache/yum /root/.cache mariadb_repo_setup; \
    fi

USER 1001

```

To fully benefit from the checksum verification, set `MARIADB_REPO_SETUP_SHA256` (and optionally override `MARIADB_REPO_SETUP_VERSION`) as build arguments in your CI/CD pipeline or build command, using the official checksum published by MariaDB for the pinned `mariadb_repo_setup` version.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread Containerfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ok-to-test Proceed with CI testing security technical-debt Code that works but needs improvement (untested, deprecated patterns, etc.)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant