|
1 | | -FROM python:3.12-slim |
| 1 | +FROM python:3.12-slim-bookworm |
2 | 2 |
|
3 | 3 | ENV PYTHONUNBUFFERED=true |
4 | 4 |
|
5 | 5 | WORKDIR /app |
6 | 6 |
|
7 | | -### install poetry |
8 | | -RUN pip install poetry && poetry config virtualenvs.in-project true |
| 7 | +# Install uv |
| 8 | +# Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv |
| 9 | +COPY --from=ghcr.io/astral-sh/uv:0.5.11 /uv /uvx /bin/ |
9 | 10 |
|
10 | | -### install dependencies and project |
| 11 | +# Place executables in the environment at the front of the path |
| 12 | +# Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-environment |
| 13 | +ENV PATH="/app/.venv/bin:$PATH" |
| 14 | + |
| 15 | +# Compile bytecode |
| 16 | +# Ref: https://docs.astral.sh/uv/guides/integration/docker/#compiling-bytecode |
| 17 | +ENV UV_COMPILE_BYTECODE=1 |
| 18 | + |
| 19 | +# uv Cache |
| 20 | +# Ref: https://docs.astral.sh/uv/guides/integration/docker/#caching |
| 21 | +ENV UV_LINK_MODE=copy |
| 22 | + |
| 23 | +# Install dependencies |
| 24 | +# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers |
| 25 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 26 | + --mount=type=bind,source=uv.lock,target=uv.lock \ |
| 27 | + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
| 28 | + uv sync --frozen --no-install-project |
| 29 | + |
| 30 | +# Copy the project into the image |
11 | 31 | ADD pyproject.toml README.md ./ |
12 | 32 | ADD app /app/app |
13 | | -RUN poetry install --no-interaction --no-ansi |
14 | 33 |
|
15 | | -### add executables to path |
16 | | -ENV PATH="/app/.venv/bin:$PATH" |
| 34 | +# Sync the project |
| 35 | +# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers |
| 36 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 37 | + uv sync |
17 | 38 |
|
18 | | -### default cmd: run fastapi with 4 workers |
| 39 | +# default cmd: run fastapi with 4 workers |
19 | 40 | CMD ["fastapi", "run", "--workers", "4", "app/main.py"] |
0 commit comments