Skip to content

Docker: split Dockerfile into multi-stage build to shrink runtime image #13

Description

@csparker247

Background

The Docker image added in #12 is a single-stage build. The final image therefore ships the entire build toolchain — build-essential, cmake, ninja, git, the -dev packages for VTK/OpenCV/Boost/TIFF/spdlog/Eigen, plus everything needed to compile ITK from source — even though none of that is needed at runtime. (The ITK and toolkit source/build trees are already cleaned up; this is about the apt build dependencies and headers.)

Proposal

Split the Dockerfile into a builder stage and a runtime stage:

# ---- builder: compilers, headers, sources ----
FROM ${BASE_IMAGE} AS builder
RUN apt install ... build-essential cmake ninja git lib*-dev
RUN git clone ITK && cmake --install ... /usr/local
COPY . /src
RUN cmake --install ... /usr/local

# ---- runtime: runtime libs + installed artifacts only ----
FROM ${BASE_IMAGE} AS runtime
RUN apt install ... imagemagick libvtk9.1 libopencv-core4.5d ...   # runtime libs, no -dev, no compilers
COPY --from=builder /usr/local /usr/local
RUN ldconfig
CMD ["rt_register", "--help"]

Expected gain

Dropping the compiler toolchain, ITK source build, and -dev packages should cut the final image by several hundred MB (likely >1 GB).

Work / risks

  • Enumerate the runtime package names (e.g. libvtk9.1, libopencv-imgproc4.5d, libboost-program-options1.83.0, libtiff6, libspdlog1.x) instead of the -dev metapackages. These names can differ across Ubuntu releases and occasionally between amd64/arm64.
  • Requires a real build + run test on both amd64 and arm64 to confirm nothing is missing at load time (ldd on the rt_* binaries is a good check).
  • Orthogonal to the build-cache work already in Add Docker image build and CI workflow #12 — this is purely about final image size.

Deferred from #12 review as a follow-up so it can get proper cross-arch verification.

Activity

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

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