Skip to content

Refactor Images to Multi-Stage Builds #54

@jan-abel-inwt

Description

@jan-abel-inwt

The r-base image has been refactored to implement a multi-stage build pattern and enforce non-root user execution, significantly improving image security, size, and build efficiency with 7d6f819.

Changes Implemented

Feature Description Benefit
Multi-Stage Build Separated the build environment (Stage 1: builder) from the runtime environment (Stage 2: final). Drastic reduction in final image size by discarding build dependencies (e.g., autoconf, cmake, apt caches) and improving layer caching.
Non-Root User Created a dedicated unprivileged user, user (UID 1000/GID 1000), for container runtime. The user is added to the staff group (see rocker) to ensure write access to /usr/local/lib/R/site-library. Adheres to the Principle of Least Privilege, significantly mitigating potential security risks if code is exploited at runtime.
Dependency Management Ensured only necessary runtime scripts (build.R, check.R, validate-settings.R, etc.) and installed packages are carried over to the final image. Further reduces image size and tightens security by removing build-only clutter.

Image Size Reduction

The multi-stage refactoring resulted in a significant reduction in the final image size, demonstrating a tangible efficiency gain.

Image Repository Tag Image ID Size Change vs. Old Build
rocker/r-ver 4.5.1 (Base Image) c71059b67e6a 948MB -
inwt/r-base 4.5.1 (Old Build) 4c69c1c07c7c 1.33GB -
inwt/r-base 4.5.1.non-root (New Multi-Stage) 9148f94c81e8 1.07GB -260MB (20% reduction)

Action Required for Dependent Images

All downstream images:

  • r-batch
  • r-shiny
  • r-model
  • r-geos

must now be updated to use the same multi-stage pattern, starting FROM inwt/r-base:4.5.1 and explicitly enforcing the non-root user for package installation and runtime.

The recommended pattern is:

  1. Stage 1 (<name>-builder): FROM inwt/r-base:4.5.1
  2. Run as root to install any new system dependencies (apt-get).
  3. Switch to user to install R packages.
  4. Stage 2 (final-<name>): FROM inwt/r-base:4.5.1
  5. COPY --from the newly installed R packages and other artifacts.
  6. USER user for final execution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions