|
1 | | -# Use an official Python runtime as a parent image |
2 | | -FROM python:3.11-slim |
| 1 | +# --- Builder Stage --- |
| 2 | +FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder |
| 3 | + |
3 | 4 | # Keeps Python from generating .pyc files in the container |
4 | 5 | ENV PYTHONDONTWRITEBYTECODE=1 |
5 | 6 |
|
6 | 7 | # Turns off buffering for easier container logging |
7 | 8 | ENV PYTHONUNBUFFERED=1 |
8 | 9 |
|
| 10 | +# Enable bytecode compilation |
| 11 | +ENV UV_COMPILE_BYTECODE=1 |
| 12 | + |
| 13 | +# Copy from the cache instead of linking since it's a mounted volume |
| 14 | +ENV UV_LINK_MODE=copy |
| 15 | + |
| 16 | +# Disable Python downloads, because we want to use the system interpreter |
| 17 | +# across both images. If using a managed Python version, it needs to be |
| 18 | +# copied from the build image into the final image; see `standalone.Dockerfile` |
| 19 | +# for an example. |
| 20 | +ENV UV_PYTHON_DOWNLOADS=0 |
| 21 | + |
9 | 22 | # Install system requirements |
10 | 23 | # gcc for C compilation |
11 | 24 | RUN apt-get -qq update && apt-get install -y gcc g++ |
12 | | -RUN apt-get -qq update && apt-get install -y git curl pipx |
| 25 | +# git for version control |
| 26 | +RUN apt-get -qq update && apt-get install -y git curl |
| 27 | + |
13 | 28 |
|
14 | 29 | # Get Rust |
15 | 30 | RUN curl https://sh.rustup.rs -sSf | bash -s -- -y |
16 | 31 | ENV PATH="${PATH}:/root/.cargo/bin" |
17 | 32 |
|
18 | | -# # Set the working directory to /app |
19 | | -WORKDIR /app |
| 33 | +# Install the project's dependencies using the lockfile and settings |
| 34 | +# TODO: Add comments on mount types and why we use them |
| 35 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 36 | + --mount=type=bind,source=uv.lock,target=uv.lock \ |
| 37 | + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
| 38 | + uv sync --locked --no-install-project --no-editable |
20 | 39 |
|
21 | | -# Install any needed packages specified in requirements.txt |
22 | | -RUN pip install -q -U pip setuptools setuptools_rust wheel |
23 | | -COPY requirements.txt /app |
24 | | -RUN pip install -q --no-cache-dir -U -r requirements.txt |
25 | 40 |
|
26 | | -# # Copy the current directory contents into the container at /app (except .dockerignore) |
| 41 | +# Copy the project into the intermediate image |
27 | 42 | COPY . /app |
28 | 43 |
|
| 44 | +# Sync the project |
| 45 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 46 | + --mount=type=bind,source=uv.lock,target=uv.lock \ |
| 47 | + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
| 48 | + uv sync --locked --no-editable |
| 49 | + |
| 50 | +# --- Final Stage --- |
| 51 | +# This stage builds the final, lean image |
| 52 | +FROM python:3.12-slim |
| 53 | + |
| 54 | +# Copy the environment, but not the source code |
| 55 | +COPY --from=builder --chown=app:app /app /app |
| 56 | + |
| 57 | +# Make sure the environment is in the PATH |
| 58 | +ENV PATH="/app/.venv/bin:$PATH" |
| 59 | + |
29 | 60 | WORKDIR /app/examples |
30 | 61 |
|
31 | 62 | # Run rpc_client.py when the container launches |
|
0 commit comments