|
1 | | -# Based on https://github.com/astral-sh/uv-docker-example/blob/main/multistage.Dockerfile |
| 1 | +########################################################### |
| 2 | +# Builder stage. Build dependencies. |
2 | 3 | FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder |
3 | | -ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy UV_PYTHON_DOWNLOADS=0 |
| 4 | +ENV PYTHONUNBUFFERED=1 \ |
| 5 | + PYTHONDONTWRITEBYTECODE=1 \ |
| 6 | + UV_COMPILE_BYTECODE=1 \ |
| 7 | + UV_LINK_MODE=copy \ |
| 8 | + UV_PYTHON_DOWNLOADS=0 |
4 | 9 |
|
5 | 10 | # pycups is sources-only so we have to build it |
6 | 11 | RUN apt-get update && apt-get install -y --no-install-recommends libcups2-dev gcc && rm -rf /var/lib/apt/lists/* |
7 | 12 |
|
8 | 13 | WORKDIR /app |
| 14 | +COPY ./pyproject.toml ./uv.lock ./ |
| 15 | + |
9 | 16 | RUN --mount=type=cache,target=/root/.cache/uv \ |
10 | | - --mount=type=bind,source=uv.lock,target=uv.lock \ |
11 | | - --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
12 | 17 | uv sync --frozen --no-install-project --no-dev |
13 | 18 |
|
14 | | -COPY . /app |
15 | | -RUN --mount=type=cache,target=/root/.cache/uv \ |
16 | | - uv sync --frozen --no-dev |
17 | 19 |
|
| 20 | +########################################################### |
| 21 | +# Production stage. Copy only runtime deps that were installed in the Builder stage. |
| 22 | +FROM python:3.12-slim-bookworm AS production |
18 | 23 |
|
19 | | -# Then, use a final image without uv |
20 | | -FROM python:3.12-slim-bookworm |
21 | | -# It is important to use the image that matches the builder, as the path to the |
22 | | -# Python executable must be the same, e.g., using `python:3.11-slim-bookworm` |
23 | | -# will fail. |
24 | | -RUN groupadd -g 1500 app && \ |
25 | | - useradd -m -u 1500 -g app app |
| 24 | +ENV PYTHONUNBUFFERED=1 |
26 | 25 |
|
27 | | -RUN apt-get update && apt-get install -y --no-install-recommends \ |
28 | | - libcups2 cups-client && rm -rf /var/lib/apt/lists/* |
| 26 | +RUN apt-get update && apt-get install -y --no-install-recommends libcups2 cups-client && rm -rf /var/lib/apt/lists/* |
29 | 27 |
|
30 | | -ENV PATH="/app/.venv/bin:$PATH" |
31 | | -# Copy the application from the builder |
32 | | -COPY --from=builder --chown=app:app /app /app |
33 | | -USER app |
| 28 | +# Copy the applicant from the builder |
| 29 | +COPY --from=builder /app /app |
| 30 | + |
| 31 | +# Create user with the name uv |
| 32 | +RUN groupadd -g 1500 uv && \ |
| 33 | + useradd -m -u 1500 -g uv uv |
| 34 | + |
| 35 | +USER uv |
34 | 36 | WORKDIR /app |
35 | 37 |
|
| 38 | +# Place executables in the environment at the front of the path |
| 39 | +ENV PATH="/app/.venv/bin:$PATH" |
| 40 | + |
| 41 | +COPY --chown=uv:uv . /app |
| 42 | + |
36 | 43 | EXPOSE 8000 |
37 | | -CMD [ "gunicorn", "--worker-class", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000", "--workers", "1", "src.api.app:app" ] |
| 44 | +CMD ["gunicorn", \ |
| 45 | + "--worker-class", "uvicorn.workers.UvicornWorker", \ |
| 46 | + "--bind", "0.0.0.0:8000", \ |
| 47 | + "--workers", "1", \ |
| 48 | + "src.api.app:app", \ |
| 49 | + "--timeout", "300", \ |
| 50 | + "--forwarded-allow-ips=*" \ |
| 51 | +] |
0 commit comments