generated from kyegomez/Python-Package-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# -------------------------------------------- | ||
# Step 1: Build Stage - Install dependencies | ||
# -------------------------------------------- | ||
FROM python:3.11-slim AS builder | ||
|
||
# Set environment variables for better production use | ||
ENV PYTHONDONTWRITEBYTECODE=1 \ | ||
PYTHONUNBUFFERED=1 \ | ||
PIP_NO_CACHE_DIR=off \ | ||
PIP_DISABLE_PIP_VERSION_CHECK=on \ | ||
PIP_DEFAULT_TIMEOUT=100 | ||
|
||
# Set working directory | ||
WORKDIR /app | ||
|
||
# Install system dependencies | ||
RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
gcc \ | ||
libpq-dev \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
# Install Python dependencies in a virtual environment | ||
RUN python -m venv /opt/venv | ||
|
||
# Copy requirements file and install dependencies | ||
COPY requirements.txt . | ||
RUN /opt/venv/bin/pip install --upgrade pip && \ | ||
/opt/venv/bin/pip install -r requirements.txt | ||
|
||
# -------------------------------------------- | ||
# Step 2: Final Stage - Copy code and run FastAPI | ||
# -------------------------------------------- | ||
FROM python:3.11-slim AS final | ||
|
||
# Set environment variables | ||
ENV PYTHONDONTWRITEBYTECODE=1 \ | ||
PYTHONUNBUFFERED=1 \ | ||
PATH="/opt/venv/bin:$PATH" | ||
|
||
# Set working directory | ||
WORKDIR /app | ||
|
||
# Copy the virtual environment from the build stage | ||
COPY --from=builder /opt/venv /opt/venv | ||
|
||
# Copy the application code | ||
COPY . . | ||
|
||
# Expose port 8000 | ||
EXPOSE 8000 | ||
|
||
# Run the application with Uvicorn, referencing api.py as the entry point | ||
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"] |