Update Dockerfile
Browse files- Dockerfile +29 -7
Dockerfile
CHANGED
|
@@ -1,18 +1,40 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Set working directory
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
COPY requirements.txt .
|
| 9 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Copy app
|
| 12 |
COPY main.py .
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
# Expose FastAPI port
|
| 15 |
EXPOSE 8000
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================
|
| 2 |
+
# Stage 1: Builder
|
| 3 |
+
# =============================================
|
| 4 |
+
FROM python:3.11-slim AS builder
|
| 5 |
|
|
|
|
| 6 |
WORKDIR /app
|
| 7 |
|
| 8 |
+
# Install build dependencies
|
| 9 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 10 |
+
gcc \
|
| 11 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
+
|
| 13 |
+
# Copy and install Python deps
|
| 14 |
COPY requirements.txt .
|
| 15 |
+
RUN pip install --no-cache-dir --user -r requirements.txt
|
| 16 |
+
|
| 17 |
+
# =============================================
|
| 18 |
+
# Stage 2: Final Runtime
|
| 19 |
+
# =============================================
|
| 20 |
+
FROM python:3.11-slim
|
| 21 |
+
|
| 22 |
+
WORKDIR /app
|
| 23 |
+
|
| 24 |
+
# Copy installed packages from builder
|
| 25 |
+
COPY --from=builder /root/.local /root/.local
|
| 26 |
+
ENV PATH=/root/.local/bin:$PATH
|
| 27 |
|
| 28 |
# Copy app
|
| 29 |
COPY main.py .
|
| 30 |
|
| 31 |
+
# Enable unbuffered output
|
| 32 |
+
ENV PYTHONUNBUFFERED=1
|
| 33 |
+
|
| 34 |
# Expose FastAPI port
|
| 35 |
EXPOSE 8000
|
| 36 |
|
| 37 |
+
# =============================================
|
| 38 |
+
# Auto-detect CPU cores and set Uvicorn workers
|
| 39 |
+
# =============================================
|
| 40 |
+
CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port 8000 --workers $(( $(nproc) * 2 ))"]
|