build(docker): downsize the Infrahub container image (~40%)

Merged
FA
4mo ago
+77-36

Why

The Infrahub container image built by invoke dev.build was ~1.37 GB. Most of that was overhead unrelated to running the server: the full C build toolchain shipped in the runtime layer, plus two heavy Python libraries that the server never imports.

Goal: shrink the image (faster pulls, smaller attack surface, lower registry/storage cost) with no change to runtime behavior.

Non-goals: no base-image swap (stays on python-slim), no change to the in-app docs/frontend bundles, no SDK code changes.

What changed

Image size: 1.37 GB -> 0.81 GB (-559 MB, ~41%), measured uniformly via docker history. Delivered as 3 independent commits:

  • build-essential + pkg-config + uv no longer ship in the image. Dependency resolution moves to a dedicated builder stage; the final backend stage is a fresh python-slim carrying only tini/git/curl/ca-certificates plus the prebuilt /.venv and /source. (-344 MB)
  • numpy is no longer installed — it was an unused runtime dependency; moved to the dev group. (-58 MB)
  • pyarrow is no longer installed by default — it's only used by the SDK's infrahubctl object load importer, never by the server/worker. Moved to a new object-transfer optional extra. (-149 MB)

What stayed the same: editable installs, hatch-vcs git-based versioning (.git bind-mount preserved in the builder stage), the git credential helper, all env vars, and the tini entrypoint. No schema/API changes.

How to review

Start with development/Dockerfile (the stage split), then the two pyproject.toml/uv.lock dependency moves.

How to test

uv run invoke dev.build
# verify the slim runtime imports the heavy compiled extensions + app:
docker run --rm --entrypoint python <image> -c "import grpc, cryptography, neo4j, asyncpg, uvloop, hiredis, bcrypt, pydantic_core, infrahub.server; print('OK')"
# pyarrow/numpy absent; infrahubctl degrades gracefully:
docker run --rm --entrypoint infrahubctl <image> --help   # -> "install the 'ctl' extra of infrahub-sdk"

Verified locally: both intermediate and final images build; all heavy extensions + infrahub.server/CLI/git.integrator import with pyarrow/numpy absent; infrahubctl prints the built-in ctl-extra hint.

Impact & rollout

  • Backward compatibility: No runtime behavior change. Only caveat: infrahubctl object load inside the container now needs infrahub-sdk[ctl] (or the object-transfer extra) installed — it exits with that exact hint otherwise.
  • Deployment notes: Safe to deploy.

Checklist

  • Changelog entries added (3 housekeeping fragments)
  • I have reviewed AI generated content

Summary by cubic

Shrink the Infrahub Docker image from 1.37 GB to 0.81 GB (~41%) by moving the build toolchain (build-essential, pkg-config, uv) into a dedicated builder stage and switching the runtime to a fresh python-slim that only ships tini, git, curl, ca-certificates, plus the prebuilt /.venv and /source. No runtime behavior changes.

  • Dependencies

    • numpy removed from runtime; now dev-only.
    • pyarrow moved to the object-transfer extra; not installed by default.
    • Updated python_sdk to lazy-load pyarrow so infrahubctl works without it; only object load needs it.
  • Migration

    • If you run infrahubctl object load in the container, install infrahub-sdk[ctl] or enable the object-transfer extra.