|
3 | 3 | ARG PYTHON_VERSION=3.11 |
4 | 4 | FROM python:${PYTHON_VERSION} AS developer |
5 | 5 |
|
6 | | -# Add any system dependencies for the developer/build environment here |
7 | | -RUN apt-get update && apt-get install -y --no-install-recommends \ |
8 | | - graphviz \ |
9 | | - && rm -rf /var/lib/apt/lists/* |
10 | | - |
11 | | -# Install uv using the official installer script |
12 | | -RUN curl -LsSf https://astral.sh/uv/install.sh | \ |
13 | | - env UV_INSTALL_DIR="/usr/local/bin" sh |
| 6 | +# Use this version of uv |
| 7 | +ARG UV_VERSION=0.7.4 |
14 | 8 |
|
15 | | -# Configure environment |
16 | | -ENV UV_CHECK_UPDATE=false |
| 9 | +# Add any system dependencies for the developer/build environment here |
| 10 | +# RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 11 | +# graphviz |
17 | 12 |
|
18 | | -# Create virtual environment |
19 | | -RUN uv venv --seed venv |
20 | | -ENV VIRTUAL_ENV=/venv |
21 | | -ENV PATH=$VIRTUAL_ENV/bin:$PATH{% if docker %} |
| 13 | +# Install uv using the official image |
| 14 | +# See https://docs.astral.sh/uv/guides/integration/docker/#installing-uv |
| 15 | +COPY --from=ghcr.io/astral-sh/uv:${UV_VERSION} /uv /uvx /bin/ |
22 | 16 |
|
23 | 17 | # The build stage installs the context into the venv |
24 | 18 | FROM developer AS build |
25 | 19 | COPY . /context |
26 | 20 | WORKDIR /context |
27 | 21 |
|
28 | | -# install dependencies and project into the local packages directory |
29 | | -RUN touch dev-requirements.txt && uv pip install -c dev-requirements.txt . |
| 22 | +# Enable bytecode compilation and copy from the cache instead of linking |
| 23 | +# since it's a mounted volume |
| 24 | +ENV UV_COMPILE_BYTECODE=1 |
| 25 | +ENV UV_LINK_MODE=copy |
| 26 | + |
| 27 | +# Install the project's dependencies using the lockfile and settings |
| 28 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 29 | + --mount=type=bind,source=uv.lock,target=uv.lock \ |
| 30 | + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
| 31 | + uv sync --locked --no-install-project --no-dev |
| 32 | + |
| 33 | +# Then, add the rest of the project source code and install it |
| 34 | +# Installing separately from its dependencies allows optimal layer caching |
| 35 | +COPY . /context |
| 36 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 37 | + uv sync --locked --no-dev |
30 | 38 |
|
31 | 39 | # The runtime stage copies the built venv into a slim runtime container |
32 | 40 | FROM python:${PYTHON_VERSION}-slim AS runtime |
33 | 41 | # Add apt-get system dependecies for runtime here if needed |
34 | | -COPY --from=build /venv/ .venv/ |
35 | | -ENV VIRTUAL_ENV=./.venv |
36 | | -ENV PATH=$VIRTUAL_ENV/bin:$PATH |
37 | 42 |
|
38 | | -# change this entrypoint if it is not the same as the repo |
| 43 | +# We need to keep the venv at the same absolute path as in the build stage |
| 44 | +COPY --from=build /context/venv/ /context/venv/ |
| 45 | +ENV PATH=/context/venv/bin:$PATH |
| 46 | + |
| 47 | +# Change this entrypoint if it is not the same as the repo |
39 | 48 | ENTRYPOINT ["{{ repo_name }}"] |
40 | 49 | CMD ["--version"]{% endif %} |
0 commit comments