Repository navigation
refactor(docker): slim multi-stage build, drop image size by ~58% - #233
Merged
Merged
Conversation
Rework the Dockerfile into a real two-stage build: - builder stage: python:3.13-slim + build toolchain, installs all requirements into an isolated venv at /opt/venv - dev stage: python:3.13-slim with only runtime packages (postgresql-client) and the interactive tooling the dev shell needs (fish, neovim, less, ack, iputils-ping), copying the venv from the builder The build toolchain (build-essential, gcc) no longer ships in the final image, cutting it from ~1.69GB to ~715MB. Other fixes: - drop the stray `aws` package from the install line (junk PyPI package; boto3 already provides the AWS SDK) - stop reinstalling ruff via pip -U (it is already pinned in dev.txt) - switch ADD to COPY for the requirements files - add apt cache mounts for /var/lib/apt/lists with sharing=locked - add a .dockerignore to shrink the build context and keep .env/secrets out Add a .dockerignore covering VCS, Python caches, local databases, env files and tooling output.
matrixise
force-pushed
the
worktree-dockerfile-multistage-slim
branch
from
September 25, 2026 05:06
5579a51 to
1afd9c4
Compare
Add a new `docker:build:cached` task that reuses the Docker build cache, complementing the existing `docker:build:no-cache` task for scenarios where faster rebuilds are preferred over guaranteeing fresh dependencies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reworks the
Dockerfileinto a real two-stage build and trims the image from ~625 MB to ~233 MB (-63%), while switching dependency installation touv syncagainstpyproject.toml+uv.lock(following the project-wide migration in #247).Changes
builderstage (python:3.13-slim+ build toolchain) resolves and installs every dependency group into an isolated venv at/opt/venv; the finaldevstage (python:3.13-slim) copies only that venv plus runtime/interactive tooling.build-essential/gccno longer ship in the final image.uvbinary is copied from the officialghcr.io/astral-sh/uv:0.12.1image (matching the version pinned inmise.toml) instead of being pip-installed;uv sync --frozen --all-groupsinstalls into a dedicated venv viaUV_PROJECT_ENVIRONMENT=/opt/venv, with a uv cache mount for fast rebuilds.python:3.13→python:3.13-slim./var/lib/apt/listswithsharing=locked..dockerignoreto shrink the build context and keep.env/secrets,.venv/, and cache directories out of it, while keepingpyproject.tomlanduv.lockin.task docker:build:cached: builds the image reusing Docker's build cache instead of--no-cache, for faster local rebuilds. The existingtask docker:buildkeeps its--no-cachebehavior unchanged.The final
devstage stays the last stage, sodocker build -t python.ie/website-dev .(used bytask docker:build, no--target) keeps producing the image consumed by both thewebandtestcompose services, with no changes needed todocker-compose.ymlinvocations.Validation
docker buildsucceeds, both with and without the build cache.docker historyon the final image shows no trace ofgcc/build-essential/uv(they only exist in thebuilderstage, which was verified to build standalone viadocker build --target builder).python pythonie/manage.py check --settings=pythonie.settings.testsreports 0 issues.task tests(7/7).uv run ruff format --check pythonieanduv run ruff check pythonieboth pass inside the container.Image size: ~625 MB → ~233 MB (built from
python:3.13single-stage vs. this multi-stagepython:3.13-slimbuild).