| 
1 |  | -# --------- requirements ---------  | 
 | 1 | +# --------- Builder Stage ---------  | 
 | 2 | +FROM ghcr.io/astral-sh/uv:python3.11-bookworm-slim AS builder  | 
2 | 3 | 
 
  | 
3 |  | -FROM python:3.11 as requirements-stage  | 
 | 4 | +# Set environment variables for uv  | 
 | 5 | +ENV UV_COMPILE_BYTECODE=1  | 
 | 6 | +ENV UV_LINK_MODE=copy  | 
4 | 7 | 
 
  | 
5 |  | -WORKDIR /tmp  | 
 | 8 | +WORKDIR /app  | 
6 | 9 | 
 
  | 
7 |  | -RUN pip install poetry poetry-plugin-export  | 
 | 10 | +# Install dependencies first (for better layer caching)  | 
 | 11 | +RUN --mount=type=cache,target=/root/.cache/uv \  | 
 | 12 | +    --mount=type=bind,source=uv.lock,target=uv.lock \  | 
 | 13 | +    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \  | 
 | 14 | +    uv sync --locked --no-install-project  | 
8 | 15 | 
 
  | 
9 |  | -COPY ./pyproject.toml ./poetry.lock* /tmp/  | 
 | 16 | +# Copy the project source code  | 
 | 17 | +COPY . /app  | 
10 | 18 | 
 
  | 
11 |  | -RUN poetry export -f requirements.txt --output requirements.txt --without-hashes  | 
 | 19 | +# Install the project in non-editable mode  | 
 | 20 | +RUN --mount=type=cache,target=/root/.cache/uv \  | 
 | 21 | +    uv sync --locked --no-editable  | 
12 | 22 | 
 
  | 
 | 23 | +# --------- Final Stage ---------  | 
 | 24 | +FROM python:3.11-slim-bookworm  | 
13 | 25 | 
 
  | 
14 |  | -# --------- final image build ---------  | 
15 |  | -FROM python:3.11  | 
 | 26 | +# Create a non-root user for security  | 
 | 27 | +RUN groupadd --gid 1000 app \  | 
 | 28 | +    && useradd --uid 1000 --gid app --shell /bin/bash --create-home app  | 
16 | 29 | 
 
  | 
17 |  | -WORKDIR /code  | 
 | 30 | +# Copy the virtual environment from the builder stage  | 
 | 31 | +COPY --from=builder --chown=app:app /app/.venv /app/.venv  | 
18 | 32 | 
 
  | 
19 |  | -COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt  | 
 | 33 | +# Ensure the virtual environment is in the PATH  | 
 | 34 | +ENV PATH="/app/.venv/bin:$PATH"  | 
20 | 35 | 
 
  | 
21 |  | -RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt  | 
 | 36 | +# Switch to the non-root user  | 
 | 37 | +USER app  | 
22 | 38 | 
 
  | 
23 |  | -COPY ./src/app /code/app  | 
 | 39 | +# Set the working directory  | 
 | 40 | +WORKDIR /code  | 
24 | 41 | 
 
  | 
25 | 42 | # -------- replace with comment to run with gunicorn --------  | 
26 | 43 | CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]  | 
27 |  | -# CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker". "-b", "0.0.0.0:8000"]  | 
 | 44 | +# CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "-b", "0.0.0.0:8000"]  | 
0 commit comments