Niansuh commited on
Commit
60eeb4d
·
verified ·
1 Parent(s): e0f8624

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +20 -14
Dockerfile CHANGED
@@ -6,46 +6,52 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
6
  build-essential \
7
  && rm -rf /var/lib/apt/lists/*
8
 
9
- # Set environment variables for user installation
10
- ENV PATH=/root/.local/bin:$PATH
 
11
 
12
- # Set the working directory in the builder
13
  WORKDIR /app
14
 
15
  # Copy the requirements file first for better caching
16
  COPY requirements.txt /app/
17
 
18
- # Install dependencies to /root/.local using --user
19
  RUN pip install --no-cache-dir --upgrade pip
20
- RUN pip install --no-cache-dir --user -r requirements.txt
21
 
22
  # Stage 2: Production
23
  FROM python:3.10-slim
24
 
25
- # Install system dependencies
26
  RUN apt-get update && apt-get install -y --no-install-recommends \
27
  libpq-dev \
28
  && rm -rf /var/lib/apt/lists/*
29
 
30
- # Set environment variables for user installation
31
- ENV PATH=/root/.local/bin:$PATH
 
 
32
  ENV PYTHONUNBUFFERED=1
33
  ENV PYTHONDONTWRITEBYTECODE=1
34
 
35
- # Set the working directory in the container
36
  WORKDIR /app
37
 
38
  # Copy installed Python packages from the builder stage
39
- COPY --from=builder /root/.local /root/.local
40
 
41
  # Copy the current directory contents into the container
42
  COPY . /app
43
 
44
- # Ensure that the Python path includes the local packages
45
- ENV PYTHONPATH=/root/.local/lib/python3.10/site-packages:$PYTHONPATH
 
 
 
46
 
47
- # Expose the port that the FastAPI app runs on
48
  EXPOSE 8001
49
 
50
- # Command to run the app with Gunicorn and Uvicorn workers
51
  CMD ["gunicorn", "-k", "uvicorn.workers.UvicornWorker", "--workers", "4", "--bind", "0.0.0.0:8001", "main:app"]
 
6
  build-essential \
7
  && rm -rf /var/lib/apt/lists/*
8
 
9
+ # Set environment variables
10
+ ENV PYTHONUNBUFFERED=1
11
+ ENV PYTHONDONTWRITEBYTECODE=1
12
 
13
+ # Set the working directory
14
  WORKDIR /app
15
 
16
  # Copy the requirements file first for better caching
17
  COPY requirements.txt /app/
18
 
19
+ # Install dependencies system-wide
20
  RUN pip install --no-cache-dir --upgrade pip
21
+ RUN pip install --no-cache-dir -r requirements.txt
22
 
23
  # Stage 2: Production
24
  FROM python:3.10-slim
25
 
26
+ # Install system dependencies required for production
27
  RUN apt-get update && apt-get install -y --no-install-recommends \
28
  libpq-dev \
29
  && rm -rf /var/lib/apt/lists/*
30
 
31
+ # Create a non-root user and group
32
+ RUN groupadd -r appuser && useradd -r -g appuser appuser
33
+
34
+ # Set environment variables
35
  ENV PYTHONUNBUFFERED=1
36
  ENV PYTHONDONTWRITEBYTECODE=1
37
 
38
+ # Set the working directory
39
  WORKDIR /app
40
 
41
  # Copy installed Python packages from the builder stage
42
+ COPY --from=builder /usr/local /usr/local
43
 
44
  # Copy the current directory contents into the container
45
  COPY . /app
46
 
47
+ # Change ownership to the non-root user
48
+ RUN chown -R appuser:appuser /app
49
+
50
+ # Switch to the non-root user
51
+ USER appuser
52
 
53
+ # Expose the port
54
  EXPOSE 8001
55
 
56
+ # Run gunicorn
57
  CMD ["gunicorn", "-k", "uvicorn.workers.UvicornWorker", "--workers", "4", "--bind", "0.0.0.0:8001", "main:app"]