Spaces:
Runtime error
Runtime error
Commit ·
456b2dd
1
Parent(s): 8d00f9e
feat: Add Dockerfile for building and running FastAPI application
Browse files- Dockerfile +42 -0
Dockerfile
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Stage 1: Build stage - Install dependencies
|
| 2 |
+
# Using a specific version is good practice for reproducibility.
|
| 3 |
+
FROM python:3.11-slim AS builder
|
| 4 |
+
|
| 5 |
+
# Set environment variables
|
| 6 |
+
ENV PYTHONDONTWRITEBYTECODE 1
|
| 7 |
+
ENV PYTHONUNBUFFERED 1
|
| 8 |
+
|
| 9 |
+
# Set the working directory
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
# Install poetry for dependency management (a robust alternative to pip)
|
| 13 |
+
# If you stick with requirements.txt, you can replace this with pip install.
|
| 14 |
+
COPY requirements.txt .
|
| 15 |
+
|
| 16 |
+
# Install dependencies without caching to keep the layer small
|
| 17 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 18 |
+
pip install --no-cache-dir -r requirements.txt
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Stage 2: Final stage - Create the production image
|
| 22 |
+
FROM python:3.11-slim
|
| 23 |
+
|
| 24 |
+
# Set the working directory
|
| 25 |
+
WORKDIR /app
|
| 26 |
+
|
| 27 |
+
# Copy the installed dependencies from the builder stage
|
| 28 |
+
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
|
| 29 |
+
COPY --from=builder /usr/local/bin /usr/local/bin
|
| 30 |
+
|
| 31 |
+
# Copy the application source code
|
| 32 |
+
COPY . .
|
| 33 |
+
|
| 34 |
+
# Expose the port that the application will run on
|
| 35 |
+
# As per your README, the FastAPI app runs on port 8000.
|
| 36 |
+
EXPOSE 8000
|
| 37 |
+
|
| 38 |
+
# Command to run the application
|
| 39 |
+
# We use Gunicorn as a process manager and Uvicorn for the ASGI server,
|
| 40 |
+
# which is the recommended setup for running FastAPI in production.
|
| 41 |
+
# The number of workers (-w) can be adjusted based on your server's CPU cores.
|
| 42 |
+
CMD ["gunicorn", "-k", "uvicorn.workers.UvicornWorker", "-w", "4", "--bind", "0.0.0.0:8000", "app:app"]
|