Mbonea commited on
Commit
dc746f2
·
1 Parent(s): 29a0e97

Simplifies Dockerfile and switches to Python 3.10

Browse files

Consolidates build and runtime stages into a single image,
removes unnecessary build tools and wheel creation, and
creates a non-root user for better security. Updates the
base image to Python 3.10, changes the working directory,
and exposes port 7860 to match the application's runtime
configuration. Streamlines the build process for faster
deployments and easier maintenance.

Files changed (1) hide show
  1. Dockerfile +18 -66
Dockerfile CHANGED
@@ -1,77 +1,29 @@
1
- # -----------------------------------------------------------------------------
2
- # Stage 1: Builder Stage
3
- # This stage installs build-time dependencies and compiles Python packages
4
- # into wheels, which can be installed in the final stage without build tools.
5
- # -----------------------------------------------------------------------------
6
- FROM python:3.11-slim-bullseye AS builder
7
 
8
- # Set environment variables
9
- ENV PYTHONDONTWRITEBYTECODE 1
10
- ENV PYTHONUNBUFFERED 1
11
 
12
- WORKDIR /app
 
13
 
14
- # Install system dependencies required for building some of the Python packages
15
- # (e.g., pandas, curl_cffi)
16
- RUN apt-get update && apt-get install -y --no-install-recommends \
17
- build-essential \
18
- libcurl4-openssl-dev \
19
- && apt-get clean \
20
- && rm -rf /var/lib/apt/lists/*
21
 
22
  # Install Python dependencies
23
- COPY requirements.txt .
24
- RUN pip install --upgrade pip
25
- # Create a wheelhouse for all dependencies
26
- RUN pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt
27
 
28
 
29
- # -----------------------------------------------------------------------------
30
- # Stage 2: Final Stage
31
- # This is the final, lean image. It copies the pre-built wheels from the
32
- # builder stage and runs the application.
33
- # -----------------------------------------------------------------------------
34
- FROM python:3.11-slim-bullseye
35
 
36
- # Set environment variables
37
- ENV PYTHONDONTWRITEBYTECODE 1
38
- ENV PYTHONUNBUFFERED 1
39
 
40
- WORKDIR /app
41
 
42
- # Install only the runtime system dependencies needed
43
- # libcurl4 is the runtime library for curl_cffi
44
- RUN apt-get update && apt-get install -y --no-install-recommends \
45
- libcurl4 \
46
- && apt-get clean \
47
- && rm -rf /var/lib/apt/lists/*
48
 
49
- # Copy the pre-built wheels from the builder stage
50
- COPY --from=builder /wheels /wheels
51
-
52
- # Install the Python dependencies from the wheels
53
- # This is much faster and doesn't require build tools in the final image
54
- RUN pip install --no-cache /wheels/*
55
-
56
- # Create a non-root user for security
57
- RUN useradd -m -U -d /home/appuser appuser
58
- USER appuser
59
- WORKDIR /home/appuser
60
-
61
- # Copy the application code into the container
62
- # NOTE: The path 'my_app' should match your application's directory name
63
- COPY --chown=appuser:appuser ./my_app .
64
-
65
- # Expose the port the app runs on
66
- EXPOSE 8000
67
-
68
- # --- Database & Migrations Note ---
69
- # The SQLite DB will be created inside the container.
70
- # For persistence, mount a volume, e.g., -v ./my_data:/home/appuser/my_data
71
- # Migrations should be run manually after starting the container.
72
- # Example: docker exec <container_name> aerich upgrade
73
-
74
- # Command to run the application
75
- # Assumes your main file is `main.py` and your FastAPI instance is `app`.
76
- # Change `main:app` if your file/variable names are different.
77
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
 
1
+ # Builder stage
2
+ FROM python:3.10.0 as builder
 
 
 
 
3
 
4
+ # Create a non-root user
5
+ RUN useradd -ms /bin/bash admin
 
6
 
7
+ # Set the working directory
8
+ WORKDIR /srv
9
 
10
+ # Copy requirements file first to leverage caching
11
+ COPY --chown=admin requirements.txt .
 
 
 
 
 
12
 
13
  # Install Python dependencies
14
+ RUN pip install --no-cache-dir -r requirements.txt
 
 
 
15
 
16
 
17
+ # Copy the application code
18
+ COPY --chown=admin . /srv
 
 
 
 
19
 
 
 
 
20
 
21
+ # Give read and write permissions to the admin user
22
 
23
+ RUN chown -R admin:admin /srv
24
+ RUN chmod 755 /srv
25
+ USER admin
26
+ CMD python -m uvicorn main:app --workers 1 --host 0.0.0.0 --port 7860 --log-level debug
 
 
27
 
28
+ # Expose port
29
+ EXPOSE 7860