rkihacker commited on
Commit
73f517b
·
verified ·
1 Parent(s): 69dab88

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +31 -24
Dockerfile CHANGED
@@ -1,41 +1,48 @@
1
  # --- Stage 1: Build Stage ---
2
- # Use a full Python image to build our dependencies.
3
  FROM python:3.11-slim as builder
4
 
5
- # Set the working directory.
6
- WORKDIR /app
7
 
8
- # Install poetry for dependency management.
9
- # Using poetry is a best practice for managing dependencies.
10
- RUN pip install poetry
11
 
12
- # Copy only the files needed for dependency installation.
13
- # This leverages Docker's layer caching.
14
- COPY pyproject.toml poetry.lock ./
 
 
 
 
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 a slim Python image for the final container.
23
  FROM python:3.11-slim
24
 
25
- # Set the working directory.
26
- WORKDIR /app
 
 
 
 
 
 
 
27
 
28
- # Copy the installed dependencies from the builder stage.
29
- COPY --from=builder /app/.venv /.venv
 
30
 
31
- # Set the PATH to include the virtual environment's bin directory.
32
- ENV PATH="/app/.venv/bin:$PATH"
33
 
34
- # Copy the application code.
35
- COPY . .
36
 
37
- # Expose the port Gunicorn will run on.
38
  EXPOSE 8000
39
 
40
- # The command to run the application using Gunicorn.
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"]