Skip to content

Commit

Permalink
Create Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
kyegomez authored Oct 8, 2024
1 parent bc114bc commit a61d722
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Dockerfile
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"]

0 comments on commit a61d722

Please sign in to comment.