@@ -12,6 +12,7 @@ FROM python:3.12-slim-bookworm AS python_builder
1212
1313# Pin uv to a specific version to make container builds reproducible.
1414ENV UV_VERSION=0.6.12
15+ ENV UV_PYTHON_DOWNLOADS=never
1516
1617# Set ENV variables that make Python more friendly to running inside a container.
1718ENV PYTHONDONTWRITEBYTECODE=1
@@ -37,32 +38,29 @@ RUN pip install "uv==${UV_VERSION}"
3738# Doing this in a multi-stage build allows omitting compile dependencies from the final image.
3839# This must be the same path that is used in the final image as the virtual environment has
3940# absolute symlinks in it.
40- ENV VIRTUAL_ENV=/opt/venv
41- RUN python -m venv ${VIRTUAL_ENV}
42- ENV PATH="${VIRTUAL_ENV}/bin:${PATH}"
41+ ENV UV_PROJECT_ENVIRONMENT=/opt/venv
42+ ENV PATH="${UV_PROJECT_ENVIRONMENT}/bin:${PATH}"
4343
4444# Copy in project dependency specification.
4545COPY pyproject.toml uv.lock ./
4646
47- # Don't install the package itself with uv because it will install it as an editable install .
47+ # Install only project dependencies, as this is cached until pyproject.toml uv.lock are updated .
4848RUN uv sync --locked --no-default-groups --no-install-project
4949
50- # Copy in source files.
50+ # Copy in source files. README is required for the package to build.
5151COPY README.md ./
5252COPY src src
5353
54- # Manually build/install the package.
55- RUN uv build && \
56- pip install dist/*.whl
54+ # Install the rest of the application into the virtual environment.
55+ RUN uv sync --locked --no-default-groups --no-editable
5756
5857# # Final Image
5958# The image used in the final image MUST match exactly to the python_builder image.
6059FROM python:3.12-slim-bookworm
6160
6261ENV PYTHONDONTWRITEBYTECODE=1
6362ENV PYTHONBUFFERED=1
64- ENV PIP_NO_CACHE_DIR=1
65- ENV VIRTUAL_ENV=/opt/venv
63+ ENV UV_PROJECT_ENVIRONMENT=/opt/venv
6664
6765ENV HOME=/home/user
6866ENV APP_HOME=${HOME}/app
@@ -84,8 +82,8 @@ RUN mkdir ${APP_HOME}
8482WORKDIR ${APP_HOME}
8583
8684# Copy and activate pre-built virtual environment.
87- COPY --from=python_builder ${VIRTUAL_ENV } ${VIRTUAL_ENV }
88- ENV PATH="${VIRTUAL_ENV }/bin:${PATH}"
85+ COPY --from=python_builder ${UV_PROJECT_ENVIRONMENT } ${UV_PROJECT_ENVIRONMENT }
86+ ENV PATH="${UV_PROJECT_ENVIRONMENT }/bin:${PATH}"
8987
9088# For Python applications that are not installable libraries, you may need to copy in source
9189# files here in the final image rather than in the python_builder image.
0 commit comments