|
1 | | -FROM python:3.11.4-slim-bullseye AS prod |
| 1 | +# Start from a slim version of Python 3.13 |
| 2 | +FROM python:3.13-slim AS base |
2 | 3 |
|
| 4 | +# Set Python Envs |
| 5 | +ENV APP_HOME=/app/ PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 UV_COMPILE_BYTECODE=1 |
3 | 6 |
|
4 | | -RUN pip install poetry==1.8.2 |
| 7 | +WORKDIR $APP_HOME |
5 | 8 |
|
6 | | -# Configuring poetry |
7 | | -RUN poetry config virtualenvs.create false |
8 | | -RUN poetry config cache-dir /tmp/poetry_cache |
| 9 | +# Install in a single layer, and clean up in the same layer to minimize image size |
| 10 | +RUN apt-get update \ |
| 11 | + && apt-get install -y --no-install-recommends \ |
| 12 | + libpq-dev \ |
| 13 | + curl \ |
| 14 | + ca-certificates \ |
| 15 | + wget \ |
| 16 | + && pip install --prefer-binary --no-cache-dir --upgrade pip \ |
| 17 | + && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ |
| 18 | + && rm -rf /var/lib/apt/lists/* \ |
9 | 19 |
|
10 | | -# Copying requirements of a project |
11 | | -COPY pyproject.toml poetry.lock /app/src/ |
12 | | -WORKDIR /app/src |
| 20 | +# Install uv |
| 21 | +# Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv |
| 22 | +COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ |
13 | 23 |
|
14 | | -# Installing requirements |
15 | | -RUN --mount=type=cache,target=/tmp/poetry_cache poetry install --only main |
| 24 | +# Place executables in the environment at the front of the path |
| 25 | +# Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-environment |
| 26 | +ENV PATH="/app/.venv/bin:$PATH" UV_LINK_MODE=copy PYTHONPATH=$APP_HOME |
16 | 27 |
|
17 | | -# Copying actuall application |
18 | | -COPY . /app/src/ |
19 | | -RUN --mount=type=cache,target=/tmp/poetry_cache poetry install --only main |
| 28 | +# ===== Prod stage ===== |
| 29 | +FROM base AS prod |
20 | 30 |
|
21 | | -CMD ["/usr/local/bin/python", "-m", "FastAPI_super_template"] |
| 31 | +# Install base dependencies |
| 32 | +# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers |
| 33 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 34 | + --mount=type=bind,source=uv.lock,target=uv.lock \ |
| 35 | + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
| 36 | + uv sync --locked --no-install-project --no-dev |
22 | 37 |
|
| 38 | +COPY ./pyproject.toml ./uv.lock $APP_HOME |
| 39 | + |
| 40 | +COPY ./src $APP_HOME/src |
| 41 | + |
| 42 | +# Sync the project |
| 43 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 44 | + uv sync --locked --no-dev |
| 45 | + |
| 46 | +# Run app - exec form (doesn’t start a shell on its own) |
| 47 | +CMD ["/usr/local/bin/python", "-m", "main.py"] |
| 48 | + |
| 49 | +# ===== Dev stage ===== |
23 | 50 | FROM prod AS dev |
24 | 51 |
|
25 | | -RUN --mount=type=cache,target=/tmp/poetry_cache poetry install |
| 52 | +RUN uv sync --locked |
0 commit comments