File size: 1,679 Bytes
cccf200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# syntax=docker/dockerfile:1

# ----------- Base image (for both build and final) -----------
FROM python:3.13-slim AS base

# ----------- Builder stage: install dependencies and venv -----------
FROM base AS builder
WORKDIR /app

# Install uv (fast Python package manager) from official container
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv

# Copy dependency files first for better cache utilization
COPY --link pyproject.toml ./
# If you have a lock file, copy it too (but not .env or secrets)
COPY --link ../uv.lock ./uv.lock

# Create virtual environment and install dependencies
ENV UV_CACHE_DIR=/root/.cache/uv
RUN --mount=type=cache,target=$UV_CACHE_DIR \
    uv venv .venv && \
    uv pip install --system --no-cache --compile -r pyproject.toml

# ----------- Final stage: minimal runtime image -----------
FROM base AS final
WORKDIR /app

# Create non-root user for security
RUN groupadd --gid 1000 appgroup && \
    useradd --uid 1000 --gid appgroup --shell /bin/bash --create-home appuser

# Copy virtual environment from builder
COPY --from=builder /app/.venv /app/.venv

# Copy application code (src/ directory and entrypoint)
COPY --link . ./

# Set environment variables
ENV PATH="/app/.venv/bin:$PATH" \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PORT=8000

# Switch to non-root user
USER appuser

# Expose port
EXPOSE 8000

# Healthcheck (optional, FastAPI default health endpoint is /health)
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:${PORT:-8000}/health || exit 1

# Start the FastAPI app with uvicorn
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]