|
| 1 | +# Stage 1: Build the Vite app |
| 2 | +FROM node:22-slim AS frontend-stage |
| 3 | + |
| 4 | +# Set the working directory inside the container |
| 5 | +COPY frontend ./ |
| 6 | + |
| 7 | +WORKDIR /frontend |
| 8 | +RUN npm install |
| 9 | +RUN npm run build |
| 10 | + |
| 11 | +# Stage 2: Build the Python application with UV |
| 12 | +FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim AS builder |
| 13 | + |
| 14 | +# Enable bytecode compilation and copy mode for the virtual environment |
| 15 | +ENV UV_COMPILE_BYTECODE=1 |
| 16 | +ENV UV_LINK_MODE=copy |
| 17 | + |
| 18 | +WORKDIR /app |
| 19 | + |
| 20 | +# Install dependencies first for better layer caching |
| 21 | +# Uses BuildKit cache mounts to speed up repeated builds |
| 22 | +RUN --mount=type=cache,target=/root/.cache/uv --mount=type=bind,source=./api_service/uv.lock,target=uv.lock --mount=type=bind,source=./api_service/pyproject.toml,target=pyproject.toml \ |
| 23 | + uv sync --locked --no-install-project --no-dev |
| 24 | + |
| 25 | +# Copy the rest of the application source and install the project |
| 26 | +COPY ./api_service /app |
| 27 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 28 | + uv sync --locked --no-dev |
| 29 | + |
| 30 | +# Stage 3: Create the final runtime image |
| 31 | +FROM python:3.13-slim-bookworm AS app |
| 32 | + |
| 33 | +COPY --from=frontend-stage /dist /app/static |
| 34 | + |
| 35 | +# ------------------------------ |
| 36 | +# 🚀 Runtime stage |
| 37 | +# ------------------------------ |
| 38 | +# Create non-root user for security |
| 39 | +RUN groupadd --system --gid 999 appuser && useradd --system --gid 999 --uid 999 --create-home appuser |
| 40 | + |
| 41 | +# Copy the application and virtual environment from builder |
| 42 | +COPY --from=builder --chown=appuser:appuser /app /app |
| 43 | + |
| 44 | +# Add virtual environment to PATH and set VIRTUAL_ENV |
| 45 | +ENV PATH=/app/.venv/bin:${PATH} |
| 46 | +ENV VIRTUAL_ENV=/app/.venv |
| 47 | +ENV PYTHONDONTWRITEBYTECODE=1 |
| 48 | +ENV PYTHONUNBUFFERED=1 |
| 49 | + |
| 50 | +# Use the non-root user to run the application |
| 51 | +USER appuser |
| 52 | + |
| 53 | +# Set working directory |
| 54 | +WORKDIR /app |
| 55 | + |
| 56 | +# Run the application |
| 57 | +ENTRYPOINT ["fastapi", "run", "app.py", "--host", "0.0.0.0", "--port", "8000"] |
0 commit comments