1- FROM python:3.11-slim AS base
1+ # Use a Python image with uv pre-installed
2+ FROM ghcr.io/astral-sh/uv:python3.11-bookworm
23
3- # Set Python environment variables
4- ENV PYTHONDONTWRITEBYTECODE=1 \
5- PYTHONUNBUFFERED=1 \
6- PYTHONFAULTHANDLER=1 \
7- LANG=C.UTF-8 \
8- LC_ALL=C.UTF-8
9-
10- # Install basic dependencies
11- RUN apt-get update && \
12- apt-get install -y --no-install-recommends \
13- gcc \
14- && rm -rf /var/lib/apt/lists/*
15-
16- # Upgrade pip
17- RUN pip install --no-cache-dir --upgrade pip
4+ # Install the project into `/app`
5+ WORKDIR /app
186
19- FROM base AS builder
7+ # Enable bytecode compilation
8+ ENV UV_COMPILE_BYTECODE=1
209
21- # Create and activate virtual environment
22- RUN python -m venv /.venv
23- ENV PATH="/.venv/bin:$PATH"
10+ # Copy from the cache instead of linking since it's a mounted volume
11+ ENV UV_LINK_MODE=copy
2412
25- # Install dependencies
26- COPY pyproject.toml .
27- RUN pip install --no-cache-dir .
13+ # Install the project's dependencies using the lockfile and settings
14+ RUN --mount=type=cache,target=/root/.cache/uv \
15+ --mount=type=bind,source=uv.lock,target=uv.lock \
16+ --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
17+ uv sync --frozen --no-install-project --no-dev
2818
29- FROM base as runtime
19+ # Then, add the rest of the project source code and install it
20+ # Installing separately from its dependencies allows optimal layer caching
21+ ADD . /app
22+ RUN --mount=type=cache,target=/root/.cache/uv \
23+ uv sync --frozen --no-dev
3024
31- # Set working directory
32- WORKDIR /app
25+ # Place executables in the environment at the front of the path
26+ ENV PATH= " /app/.venv/bin:$PATH"
3327
34- # Copy virtual environment from builder
35- COPY --from=builder /.venv /.venv
36- ENV PATH="/.venv/bin:$PATH"
28+ # Reset the entrypoint, don't invoke `uv`
29+ ENTRYPOINT []
3730
38- # Copy application code
39- COPY . .
31+ # Run setup.py
4032
41- # Expose port (adjust if needed)
42- EXPOSE 8000
33+ RUN python setup.py
4334
44- # Run the application
45- CMD ["python" , "main.py" ]
35+ # Run the FastAPI application by default
36+ # Uses `fastapi dev` to enable hot-reloading when the `watch` sync occurs
37+ # Uses `--host 0.0.0.0` to allow access from outside the container
38+ CMD ["python" , "main.py" ]
0 commit comments