1- # From https://docs.astral.sh/uv/guides/integration/docker/
2- FROM python:3.11-slim
1+ # First, build the application in the `/app` directory.
2+ FROM ghcr.io/astral-sh/uv:0.8.0-python3.12-bookworm-slim AS builder
3+ ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy
34
4- # uv installer requires curl (and certificates) to download the release archive
5- # ssh and git are required by setuptools_scm for automatic versioning using git
6- RUN apt-get -yq update && apt-get -yqq install --no-install-recommends ssh git curl ca-certificates
5+ # Disable Python downloads, because we want to use the system interpreter
6+ # across both images. If using a managed Python version, it needs to be
7+ # copied from the build image into the final image
8+ ENV UV_PYTHON_DOWNLOADS=0
79
8- # Download the latest installer
9- ADD https://astral.sh/uv/0.4.3/install.sh /uv-installer.sh
10+ # Required for setuptools_scm: https://setuptools-scm.readthedocs.io/en/stable/usage/
11+ ARG SETUPTOOLS_SCM_PRETEND_VERSION=1
1012
11- # Run the installer then remove it
12- RUN sh /uv-installer.sh && rm /uv-installer.sh
13+ WORKDIR /app
14+ RUN --mount=type=cache,target=/root/.cache/uv \
15+ --mount=type=bind,source=uv.lock,target=uv.lock \
16+ --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
17+ uv sync --frozen --no-install-project --no-dev
1318
14- # Ensure the installed binary is on the `PATH`
15- ENV PATH="/root/.cargo/bin/:$PATH"
19+ ADD . /app
20+ RUN --mount=type=cache,target=/root/.cache/uv \
21+ uv sync --frozen --no-dev
1622
17- # Change the working directory to the `app` directory
18- WORKDIR /app
1923
20- # Copy the lockfile and `pyproject.toml` into the image
21- COPY uv.lock /app/uv.lock
22- COPY pyproject.toml /app/pyproject.toml
24+ # Then, use a final image without uv
25+ FROM python:3.12-slim-bookworm
26+ # It is important to use the image that matches the builder, as the path to the
27+ # Python executable must be the same, e.g., using `python:3.11-slim-bookworm`
28+ # will fail.
2329
24- # Install dependencies
25- RUN uv sync --frozen --no-cache --no-dev --no-install-project
30+ # Copy the application from the builder
31+ COPY --from=builder --chown=app:app /app /app
2632
27- # Copy the project into the image
28- COPY . /app
33+ # Place executables in the environment at the front of the path
34+ ENV PATH= " /app/.venv/bin:$PATH"
2935
30- # Sync the project into a new environment
31- # Mounting the git directory is required by setuptools_scm: https://setuptools-scm.readthedocs.io/en/stable/usage/
32- RUN --mount=source=.git,target=.git,type=bind \
33- uv sync --frozen --no-cache --no-dev
36+ WORKDIR /app
3437
35- CMD ["uv" , "run" , "uvicorn" , "template.api:app" , "--host" , "0.0.0.0" , "--port" , "80" ]
38+ # Run the FastAPI application by default
39+ CMD ["uvicorn" , "template.api:app" , "--host" , "0.0.0.0" , "--port" , "80" ]
0 commit comments