Dockerfile: bind-mount the wheel so it stops shipping in every published image (#893)

* Dockerfile: bind-mount the wheel instead of COPYing it

The runtime stage COPYs the built wheel out of the build stage and the
RUN that installs it rm -rf's it again. A RUN cannot remove what an
earlier instruction already committed -- it writes a whiteout on top --
so the COPY layer ships in every pull.

Measured on ghcr.io/domainaware/parsedmarc:11.0.0: on amd64 layer 4 is
10,713,473 bytes of a 267,653,434-byte image (4.0%); on arm64
10,713,473 of 270,400,579 (3.96%). Layer 5 carries tmp/.wh.dist.
The 10.5.0 and 10.0.0 tags carry the same layer.

Bind mounts are not committed to layers, so the rm -rf /tmp/dist clause
is no longer needed and is removed. A # syntax=docker/dockerfile:1
directive is added so RUN --mount is guaranteed available.

* Add --no-cache-dir option to pip install

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Dockerfile: drop the syntax directive; document the size win

`RUN --mount=type=bind,from=<stage>` has been stable in BuildKit's built-in
Dockerfile frontend since 2020, and the Docker workflow builds through
docker/setup-buildx-action + docker/build-push-action, so the mount works
without `# syntax=docker/dockerfile:1`. The directive only adds an
unpinned Docker Hub round trip before the Dockerfile can be read
(`resolve image config for docker-image://docker.io/docker/dockerfile:1`),
on a floating tag, and it does not help the legacy non-BuildKit builder,
which rejects `RUN --mount` either way.

Verified on linux/amd64 with BuildKit (buildx 0.37.0, Docker 29.8.0):
building with and without the directive produced five layers totalling
163,757,439 and 163,757,152 compressed bytes respectively -- a 287-byte
gzip nondeterminism, otherwise identical. The master baseline built six
layers totalling 272,138,724 bytes, whose wheel layer of 10,713,475 bytes
reproduces the published 11.0.0 image's 10,713,473 to within two bytes.

The comment block is trimmed to state the invariant rather than narrate
what the code used to do, and CHANGELOG.md gains an entry: the change is
user-visible, and the ~40% reduction is mostly the pip download cache
that --no-cache-dir removes, not the wheel layer the PR was opened for.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Sean Whalen <44679+seanthegeek@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Sujeito Operator
2026-09-09 13:10:37 -04:00
committed by GitHub
co-authored by Claude Opus 5 Sean Whalen Copilot Autofix powered by AI
parent de88cd893f
commit e1c2e42d5d
2 changed files with 13 additions and 4 deletions
+6
View File
@@ -1,5 +1,11 @@
# Changelog
## Unreleased
### Changes
- **The prebuilt Docker image (`ghcr.io/domainaware/parsedmarc`) is roughly 40% smaller to pull** ([#893](https://github.com/domainaware/parsedmarc/pull/893)). The runtime stage copied the built wheel out of the build stage and deleted it again at the end of the next `RUN`, but a `RUN` can only write a whiteout over a layer an earlier instruction already committed: the wheel shipped in every published image and every `docker pull` downloaded it (10,713,473 bytes of the 11.0.0 image, on both architectures). The wheel is now bind-mounted from the build stage instead, and a bind mount is never committed to a layer. `pip install` also runs with `--no-cache-dir`, which drops a further ~99 MB of pip's download cache that the image had been carrying in the same layer as `site-packages`. Measured on linux/amd64: 272,138,724 compressed bytes across six layers before, 163,757,439 across five after.
## 11.0.1
### Security
+7 -4
View File
@@ -23,8 +23,12 @@ ARG USERNAME
ARG USER_UID
ARG USER_GID
COPY --from=build /app/dist/*.whl /tmp/dist/
RUN set -ex; \
# The wheel is bind-mounted from the `build` stage rather than copied in with
# COPY: a COPY commits the wheel to its own layer, which a later `rm` can only
# write a whiteout over, so the wheel would ship in every pull. A bind mount is
# never committed to a layer.
RUN --mount=type=bind,from=build,source=/app/dist,target=/tmp/dist \
set -ex; \
groupadd --gid ${USER_GID} ${USERNAME}; \
useradd --uid ${USER_UID} --gid ${USER_GID} -m ${USERNAME}; \
# Install the wheel with the [all] and [postgresql] extras so the prebuilt
@@ -38,8 +42,7 @@ RUN set -ex; \
# manylinux wheels for both amd64 and arm64, so this adds no source-build
# step on either platform.
whl="$(ls /tmp/dist/*.whl)"; \
pip install "${whl}[all,postgresql]"; \
rm -rf /tmp/dist
pip install --no-cache-dir "${whl}[all,postgresql]"
USER $USERNAME