Update Dockerfile
Browse files- Dockerfile +31 -24
Dockerfile
CHANGED
|
@@ -1,41 +1,48 @@
|
|
| 1 |
# --- Stage 1: Build Stage ---
|
| 2 |
-
# Use a
|
| 3 |
FROM python:3.11-slim as builder
|
| 4 |
|
| 5 |
-
# Set the working directory
|
| 6 |
-
WORKDIR /app
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
RUN pip install poetry
|
| 11 |
|
| 12 |
-
# Copy only the
|
| 13 |
-
# This
|
| 14 |
-
COPY
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Install dependencies.
|
| 17 |
-
# --no-root: Don't install the project itself, only the dependencies.
|
| 18 |
-
# --no-dev: Exclude development dependencies.
|
| 19 |
-
RUN poetry install --no-root --no-dev
|
| 20 |
|
| 21 |
# --- Stage 2: Runtime Stage ---
|
| 22 |
-
# Use
|
| 23 |
FROM python:3.11-slim
|
| 24 |
|
| 25 |
-
# Set
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
|
| 37 |
-
# Expose the port Gunicorn will
|
| 38 |
EXPOSE 8000
|
| 39 |
|
| 40 |
-
# The command to
|
| 41 |
CMD ["gunicorn", "-c", "gunicorn_config.py", "main:app"]
|
|
|
|
| 1 |
# --- Stage 1: Build Stage ---
|
| 2 |
+
# Use a specific Python version to ensure consistency.
|
| 3 |
FROM python:3.11-slim as builder
|
| 4 |
|
| 5 |
+
# Set the working directory
|
| 6 |
+
WORKDIR /usr/src/app
|
| 7 |
|
| 8 |
+
# Upgrade pip and install wheel to build dependencies efficiently
|
| 9 |
+
RUN pip install --upgrade pip wheel
|
|
|
|
| 10 |
|
| 11 |
+
# Copy only the requirements file to leverage Docker's layer caching.
|
| 12 |
+
# This step will only be re-run if requirements.txt changes.
|
| 13 |
+
COPY ./requirements.txt .
|
| 14 |
+
|
| 15 |
+
# Build wheel files for all dependencies. This is faster and more reliable
|
| 16 |
+
# for the final stage than a simple 'pip install'.
|
| 17 |
+
RUN pip wheel --no-cache-dir --wheel-dir /usr/src/app/wheels -r requirements.txt
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# --- Stage 2: Runtime Stage ---
|
| 21 |
+
# Use the same slim Python base image for a smaller final image size.
|
| 22 |
FROM python:3.11-slim
|
| 23 |
|
| 24 |
+
# Set a non-root user for better security
|
| 25 |
+
RUN addgroup --system app && adduser --system --group app
|
| 26 |
+
|
| 27 |
+
# Set the working directory
|
| 28 |
+
WORKDIR /home/app
|
| 29 |
+
|
| 30 |
+
# Copy the pre-built wheel dependencies from the builder stage
|
| 31 |
+
COPY --from=builder /usr/src/app/wheels /wheels
|
| 32 |
+
COPY --from=builder /usr/src/app/requirements.txt .
|
| 33 |
|
| 34 |
+
# Install the dependencies from local wheel files without needing to rebuild them.
|
| 35 |
+
# --no-index and --find-links ensure pip only uses the local wheels.
|
| 36 |
+
RUN pip install --no-cache /wheels/*
|
| 37 |
|
| 38 |
+
# Copy the application code into the container
|
| 39 |
+
COPY --chown=app:app . .
|
| 40 |
|
| 41 |
+
# Switch to the non-root user
|
| 42 |
+
USER app
|
| 43 |
|
| 44 |
+
# Expose the port Gunicorn will listen on
|
| 45 |
EXPOSE 8000
|
| 46 |
|
| 47 |
+
# The command to start the Gunicorn server
|
| 48 |
CMD ["gunicorn", "-c", "gunicorn_config.py", "main:app"]
|