-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDockerfile
More file actions
52 lines (37 loc) · 1.63 KB
/
Dockerfile
File metadata and controls
52 lines (37 loc) · 1.63 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
# - Stage 1 --------------------------------------------------------------------
FROM python:3.12-slim-bookworm AS build
WORKDIR /app
# Install build tools needed to compile some Python packages
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential gcc && \
rm -rf /var/lib/apt/lists/*
# Copy and build all required packages (with dependencies) into wheels
COPY requirements.txt .
RUN pip wheel --no-cache -r requirements.txt -w /app/wheelhouse
# Copy full app source (not strictly needed in build stage unless building static assets)
COPY . .
# - Stage 2 --------------------------------------------------------------------
FROM python:3.12-slim-bookworm AS runtime
WORKDIR /app
# Only bring in requirements and prebuilt wheels from build stage
COPY requirements.txt .
COPY --from=build /app/wheelhouse /app/wheelhouse
# Install app dependencies from local wheelhouse
RUN pip install --no-cache-dir --no-index --find-links /app/wheelhouse -r requirements.txt
# Copy only the necessary runtime source files
COPY models ./models
COPY routes ./routes
COPY schemas ./schemas
COPY services ./services
COPY data ./data
COPY main.py .
# Add non-root user for security hardening
RUN adduser --disabled-password --gecos '' fastapi && \
chown -R fastapi:fastapi /app
USER fastapi
# Prevent Python from buffering stdout/stderr
ENV PYTHONUNBUFFERED=1
# Expose FastAPI port
EXPOSE 9000
# Start the FastAPI app with Uvicorn
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"]