Harden Dockerfile for production: pin, freeze, drop runtime uv#98
Open
brandonros wants to merge 3 commits into
Open
Harden Dockerfile for production: pin, freeze, drop runtime uv#98brandonros wants to merge 3 commits into
brandonros wants to merge 3 commits into
Conversation
The previous Dockerfile was a dev setup masquerading as production: - `FROM python:latest` meant rebuilds drifted silently over time. - `uv sync --locked` + `uv run` at CMD meant every container start re-validated the lockfile against the venv, which can touch the package index (at minimum DNS) even when the venv is already built. - `--reload` is uvicorn's dev mode; it watches the filesystem and respawns workers, each of which re-invokes the CMD and compounds the above. This rewrite: - Pins the base image (python:3.12-slim) and uv (0.4.30) so builds are reproducible. - Uses a multi-stage build so the runtime image ships neither uv nor pip caches. - Switches `--locked` -> `--frozen` so lockfile drift fails the build loudly instead of being silently reconciled. - Splits the dep-install layer from the source-copy layer so code changes don't bust the dependency cache. - Drops `uv run` from CMD and invokes uvicorn directly from /app/.venv/bin via PATH. Runtime startup now makes zero PyPI callouts and needs no DNS for package management. - Removes `--reload`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Without these env vars, `uv sync` downloads its own CPython into /root/.local/share/uv/python/ and symlinks the venv's interpreter to it. In a multi-stage build we only COPY /app across, so that interpreter directory is absent from the runtime image and the venv's python symlink dangles -- exec fails at container start with "No such file or directory" on /app/.venv/bin/uvicorn. Silent version drift too: uv picked 3.10 despite the 3.12-slim base. UV_PYTHON_PREFERENCE=only-system + UV_PYTHON_DOWNLOADS=never force uv to use /usr/local/bin/python3.12 from the base image, which is present in both stages. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The host pins Python 3.10 via .python-version for local dev. Keeping the container on the same version avoids subtle behavior differences between developer machines and production (stdlib changes, dep wheel selection, etc.) and means `uv sync` respects the existing pin inside the build context without extra workarounds. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
The previous Dockerfile was a dev setup masquerading as production:
FROM python:latestmeant rebuilds drifted silently over time.uv sync --locked+uv runat CMD meant every container start re-validated the lockfile against the venv, which can touch the package index (at minimum DNS) even when the venv is already built.--reloadis uvicorn's dev mode; it watches the filesystem and respawns workers, each of which re-invokes the CMD and compounds the above.This rewrite:
--locked->--frozenso lockfile drift fails the build loudly instead of being silently reconciled.uv runfrom CMD and invokes uvicorn directly from /app/.venv/bin via PATH. Runtime startup now makes zero PyPI callouts and needs no DNS for package management.--reload.