-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDockerfile
More file actions
54 lines (40 loc) · 1.85 KB
/
Dockerfile
File metadata and controls
54 lines (40 loc) · 1.85 KB
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
# - Stage 1: Build dependencies into wheels ------------------------------------
FROM python:3.12-slim-bookworm AS build
WORKDIR /app
# Install system build tools needed to compile Python packages with native
# extensions
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential gcc && \
rm -rf /var/lib/apt/lists/*
# Pre-build all third-party dependencies into wheel files. This enables faster,
# more reliable installation later without relying on network access
COPY requirements.txt .
RUN pip wheel --no-cache-dir --wheel-dir=/app/wheelhouse -r requirements.txt
# - Stage 2: Runtime image ----------------------------------------------------
FROM python:3.12-slim-bookworm AS runtime
WORKDIR /app
# Install dependencies from prebuilt wheels (no network access)
# This improves build speed and avoids dependency drift
COPY requirements.txt .
COPY --from=build /app/wheelhouse /app/wheelhouse
RUN pip install --no-cache-dir --no-index --find-links /app/wheelhouse -r requirements.txt
# Copy only runtime-relevant application code (excluding tests and tooling)
COPY models ./models
COPY routes ./routes
COPY schemas ./schemas
COPY services ./services
COPY data ./data
COPY main.py .
# Copy README and assets needed for GHCR package page metadata
COPY README.md ./
COPY assets ./assets
# Add a non-root user for better container security
RUN adduser --disabled-password --gecos '' fastapi && \
chown -R fastapi:fastapi /app
USER fastapi
# Ensure logs and errors appear in Docker logs immediately
ENV PYTHONUNBUFFERED=1
# Expose FastAPI default port
EXPOSE 9000
# Start the FastAPI application using Uvicorn ASGI server
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"]