|
1 | | -# Install uv |
2 | | -FROM python:3.12-slim |
3 | | -COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ |
| 1 | +# First, build the application in the `/app` directory |
| 2 | +FROM ghcr.io/astral-sh/uv:bookworm-slim AS builder |
| 3 | +ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy |
4 | 4 |
|
5 | | -# Install git, gcc etc. |
6 | | -RUN apt-get update && apt-get upgrade -y && apt-get install -y git build-essential |
7 | | - |
8 | | -# Change the working directory to the `app` directory |
9 | | -WORKDIR /app |
| 5 | +# Configure the Python directory so it is consistent |
| 6 | +ENV UV_PYTHON_INSTALL_DIR=/python |
10 | 7 |
|
11 | | -# Enable bytecode compilation |
12 | | -ENV UV_COMPILE_BYTECODE=1 |
| 8 | +# Only use the managed Python version |
| 9 | +ENV UV_PYTHON_PREFERENCE=only-managed |
13 | 10 |
|
14 | | -# Copy from the cache instead of linking since it's a mounted volume |
15 | | -ENV UV_LINK_MODE=copy |
| 11 | +# Install Python before the project for caching |
| 12 | +RUN uv python install 3.12 |
16 | 13 |
|
17 | | -# Install dependencies |
| 14 | +WORKDIR /app |
18 | 15 | RUN --mount=type=cache,target=/root/.cache/uv \ |
19 | 16 | --mount=type=bind,source=uv.lock,target=uv.lock \ |
20 | 17 | --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
21 | | - uv sync -v --locked --no-install-project |
| 18 | + uv sync --locked --no-install-project --no-dev |
| 19 | +COPY . /app |
| 20 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 21 | + uv sync --locked --no-dev |
22 | 22 |
|
23 | | -# Copy contents into the image |
24 | | -ADD . /app |
| 23 | +# Then, use a final image without uv |
| 24 | +FROM debian:bookworm-slim |
25 | 25 |
|
26 | | -# Sync the project |
27 | | -RUN --mount=type=cache,target=/root/.cache/uv \ |
28 | | - uv sync --locked |
| 26 | +# Install git, gcc etc. |
| 27 | +RUN apt-get update && apt-get upgrade -y && apt-get install -y git build-essential |
| 28 | + |
| 29 | +# Setup a non-root user |
| 30 | +RUN groupadd --system --gid 999 nonroot \ |
| 31 | + && useradd --system --gid 999 --uid 999 --create-home nonroot |
| 32 | + |
| 33 | +# Copy the Python version |
| 34 | +COPY --from=builder --chown=python:python /python /python |
| 35 | + |
| 36 | +# Copy the application from the builder |
| 37 | +COPY --from=builder --chown=nonroot:nonroot /app /app |
| 38 | + |
| 39 | +# Place executables in the environment at the front of the path |
| 40 | +ENV PATH="/app/.venv/bin:$PATH" |
| 41 | + |
| 42 | +# Use the non-root user to run our application |
| 43 | +USER nonroot |
| 44 | + |
| 45 | +# Use `/app` as the working directory |
| 46 | +WORKDIR /app |
29 | 47 |
|
30 | | -# Run app.py |
31 | | -CMD ["uv", "run", "run_app.py"] |
| 48 | +# Run the application |
| 49 | +CMD ["ml_peg", "app"] |
0 commit comments