48 lines
1.4 KiB
Docker
48 lines
1.4 KiB
Docker
# --------- Builder Stage ---------
|
|
FROM ghcr.io/astral-sh/uv:python3.11-bookworm-slim AS builder
|
|
|
|
# Set environment variables for uv
|
|
ENV UV_COMPILE_BYTECODE=1
|
|
ENV UV_LINK_MODE=copy
|
|
|
|
WORKDIR /app
|
|
|
|
# Install dependencies first (for better layer caching)
|
|
RUN --mount=type=cache,target=/root/.cache/uv \
|
|
--mount=type=bind,source=uv.lock,target=uv.lock \
|
|
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
|
|
uv sync --locked --no-install-project
|
|
|
|
# Copy the project source code
|
|
COPY . /app
|
|
|
|
# Install the project in non-editable mode
|
|
RUN --mount=type=cache,target=/root/.cache/uv \
|
|
uv sync --locked --no-editable
|
|
|
|
# --------- Final Stage ---------
|
|
FROM python:3.11-slim-bookworm
|
|
|
|
# Create a non-root user for security
|
|
RUN groupadd --gid 1000 app \
|
|
&& useradd --uid 1000 --gid app --shell /bin/bash --create-home app
|
|
|
|
# Copy the virtual environment from the builder stage
|
|
COPY --from=builder --chown=app:app /app/.venv /app/.venv
|
|
|
|
# Ensure the virtual environment is in the PATH
|
|
ENV PATH="/app/.venv/bin:$PATH"
|
|
|
|
# Switch to the non-root user
|
|
USER app
|
|
|
|
# Set the working directory
|
|
WORKDIR /code
|
|
|
|
# Add /code/src to Python import path
|
|
ENV PYTHONPATH=/code/src
|
|
|
|
# -------- replace with comment to run with gunicorn --------
|
|
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
|
|
# CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "-b", "0.0.0.0:8000"]
|