ayush2917 commited on
Commit
6e6e8a8
·
verified ·
1 Parent(s): 30fcf6b

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +40 -24
Dockerfile CHANGED
@@ -1,37 +1,53 @@
1
- # Use Python 3.9 slim base image for smaller footprint
 
2
 
3
- FROM python:3.9-slim
 
 
 
 
 
 
4
 
5
- # Set working directory
 
 
6
 
7
- WORKDIR /app
 
 
8
 
9
- # Install system dependencies required for Python packages (e.g., firebase-admin, gcc for compilation)
 
10
 
 
11
  RUN apt-get update && \
12
- apt-get install -y --no-install-recommends \
13
- gcc \
14
- python3-dev \
15
- libssl-dev \
16
- libc-dev && \
17
- rm -rf /var/lib/apt/lists/\*
18
-
19
- # Copy requirements.txt first to leverage Docker cache
20
-
21
- COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt
22
 
23
- # Copy the entire application code
 
 
24
 
25
- COPY . .
 
 
 
26
 
27
- # Set environment variables
 
28
 
29
- ENV PORT=7860 PYTHONUNBUFFERED=1
30
-
31
- # Expose the port
 
 
32
 
 
33
  EXPOSE 7860
 
 
34
 
35
- # Run Gunicorn with optimized settings
36
-
37
- CMD \["python", "-m", "gunicorn", "--bind", "0.0.0.0:7860", "--workers", "2", "--threads", "4", "--timeout", "120", "app:app"\]
 
1
+ # Use Python 3.9 slim-buster for stability and smaller footprint
2
+ FROM python:3.9-slim-buster as builder
3
 
4
+ # Install system dependencies
5
+ RUN apt-get update && \
6
+ apt-get install -y --no-install-recommends \
7
+ gcc \
8
+ python3-dev \
9
+ libssl-dev \
10
+ && rm -rf /var/lib/apt/lists/*
11
 
12
+ # Create and activate virtual environment
13
+ RUN python -m venv /opt/venv
14
+ ENV PATH="/opt/venv/bin:$PATH"
15
 
16
+ # Install Python dependencies
17
+ COPY requirements.txt .
18
+ RUN pip install --no-cache-dir -r requirements.txt
19
 
20
+ # ========== Runtime Stage ========== #
21
+ FROM python:3.9-slim-buster
22
 
23
+ # Install runtime dependencies
24
  RUN apt-get update && \
25
+ apt-get install -y --no-install-recommends \
26
+ libssl1.1 \
27
+ && rm -rf /var/lib/apt/lists/*
 
 
 
 
 
 
 
28
 
29
+ # Copy virtual environment from builder
30
+ COPY --from=builder /opt/venv /opt/venv
31
+ ENV PATH="/opt/venv/bin:$PATH"
32
 
33
+ # Create non-root user
34
+ RUN useradd -m -u 1001 krishna
35
+ USER krishna
36
+ WORKDIR /home/krishna/app
37
 
38
+ # Copy application code (with proper ownership)
39
+ COPY --chown=krishna:krishna . .
40
 
41
+ # Environment variables
42
+ ENV PORT=7860 \
43
+ PYTHONUNBUFFERED=1 \
44
+ PYTHONPATH=/home/krishna/app \
45
+ GUNICORN_CMD_ARGS="--bind=0.0.0.0:7860 --workers=2 --threads=4 --timeout=120 --worker-class=gthread --log-level=info"
46
 
47
+ # Expose port and health check
48
  EXPOSE 7860
49
+ HEALTHCHECK --interval=30s --timeout=3s \
50
+ CMD curl -f http://localhost:7860/ || exit 1
51
 
52
+ # Run Gunicorn
53
+ CMD ["gunicorn", "app:app"]