prthm11 commited on
Commit
d458acb
·
verified ·
1 Parent(s): 52c4ffb

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +33 -23
Dockerfile CHANGED
@@ -61,8 +61,11 @@
61
  # CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "2", "app:app"]
62
  # #CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "", "--timeout", "360", "app:app"]
63
 
 
 
64
  FROM python:3.11-slim
65
 
 
66
  WORKDIR /app
67
 
68
  # Environment: Hugging Face cache + force CPU behavior
@@ -73,14 +76,13 @@ ENV DEBIAN_FRONTEND=noninteractive \
73
  HF_HOME=/app/cache \
74
  NLTK_DATA=/app/nltk_data \
75
  MPLCONFIGDIR=/app/.config/matplotlib \
76
- # Force CPU-only for PyTorch/transformers
 
77
  CUDA_VISIBLE_DEVICES= \
78
  PYTORCH_ENABLE_MPS=0 \
79
- XDG_CACHE_HOME=/app/.cache \
80
  PYTORCH_NO_CUDA=1
81
 
82
-
83
- # Copy only what's needed (avoid duplicate COPY . /app)
84
  COPY requirements.txt ./requirements.txt
85
  COPY app.py ./app.py
86
  COPY templates/ ./templates/
@@ -88,7 +90,7 @@ COPY utils/ ./utils/
88
  COPY blocks/ ./blocks/
89
  COPY generated_projects/ ./generated_projects/
90
 
91
- # Install system dependencies (minimal for CPU inference + common tools)
92
  RUN apt-get update && apt-get install -y --no-install-recommends \
93
  fontconfig \
94
  fonts-dejavu-core \
@@ -104,25 +106,33 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
104
  poppler-utils \
105
  && apt-get clean && rm -rf /var/lib/apt/lists/*
106
 
107
- # Python deps
108
- RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt
 
 
 
 
 
 
 
 
 
 
109
 
 
 
110
  RUN python -m nltk.downloader -d /app/nltk_data punkt averaged_perceptron_tagger wordnet || true
111
 
112
- # Create necessary directories with correct permissions
113
- RUN mkdir -p /app/nltk_data /app/.config/matplotlib \
114
- && mkdir -p /app/cache /app/data /app/logs /app/outputs /app/blocks \
115
- && mkdir -p /app/outputs/DETECTED_IMAGE /app/outputs/SCANNED_IMAGE /app/outputs/EXTRACTED_JSON \
116
- && chown -R root:root /app \
117
- && chmod -R 755 /app/cache /app/data /app/logs /app/outputs
118
-
119
- RUN mkdir -p /app/.config/matplotlib \
120
- /app/cache /app/nltk_data /nltk_data \
121
- /app/.cache/fontconfig /root/.cache/fontconfig \
122
- && chmod -R 777 /app/.config/matplotlib /app/.cache /app/cache /app/nltk_data /nltk_data /root/.cache/fontconfig
123
-
124
  RUN fc-cache -f -v || true
125
 
 
 
 
 
 
 
 
126
  # Set Flask env
127
  ENV FLASK_APP=app.py \
128
  FLASK_ENV=production
@@ -130,9 +140,9 @@ ENV FLASK_APP=app.py \
130
  # Expose port
131
  EXPOSE 7860
132
 
133
- # # Healthcheck (lightweight endpoint; change if your app uses a different path)
134
- # HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
135
- # CMD curl -f http://localhost:7860/healthz || exit 1
136
 
137
- # Single worker avoids multiple processes duplicating model memory on CPU-heavy workloads.
138
  CMD ["gunicorn", "app:app", "-b", "0.0.0.0:7860", "-w", "1", "--threads", "4", "-k", "gthread", "--timeout", "0", "--graceful-timeout", "0"]
 
61
  # CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "2", "app:app"]
62
  # #CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "", "--timeout", "360", "app:app"]
63
 
64
+ # Dockerfile — CPU-optimized, permissions-fixed, non-root runtime
65
+
66
  FROM python:3.11-slim
67
 
68
+ # Set working dir
69
  WORKDIR /app
70
 
71
  # Environment: Hugging Face cache + force CPU behavior
 
76
  HF_HOME=/app/cache \
77
  NLTK_DATA=/app/nltk_data \
78
  MPLCONFIGDIR=/app/.config/matplotlib \
79
+ XDG_CACHE_HOME=/app/.cache \
80
+ # Force CPU-only (ensure no CUDA attempts)
81
  CUDA_VISIBLE_DEVICES= \
82
  PYTORCH_ENABLE_MPS=0 \
 
83
  PYTORCH_NO_CUDA=1
84
 
85
+ # Copy minimal files first (leverage layer caching)
 
86
  COPY requirements.txt ./requirements.txt
87
  COPY app.py ./app.py
88
  COPY templates/ ./templates/
 
90
  COPY blocks/ ./blocks/
91
  COPY generated_projects/ ./generated_projects/
92
 
93
+ # Install system dependencies (including fontconfig/fonts)
94
  RUN apt-get update && apt-get install -y --no-install-recommends \
95
  fontconfig \
96
  fonts-dejavu-core \
 
106
  poppler-utils \
107
  && apt-get clean && rm -rf /var/lib/apt/lists/*
108
 
109
+ # Create runtime directories, cache dirs, and static dir BEFORE pip install to ensure permissions
110
+ # We will chown to non-root user later
111
+ RUN mkdir -p /app/.config/matplotlib \
112
+ /app/cache /app/nltk_data /nltk_data \
113
+ /app/.cache /app/.cache/fontconfig /root/.cache/fontconfig \
114
+ /app/logs /app/outputs /app/outputs/DETECTED_IMAGE /app/outputs/SCANNED_IMAGE /app/outputs/EXTRACTED_JSON \
115
+ /app/data /app/blocks /app/static \
116
+ && chmod -R 755 /app
117
+
118
+ # Install Python dependencies
119
+ RUN pip install --upgrade pip \
120
+ && pip install --no-cache-dir -r requirements.txt
121
 
122
+ # Pre-download NLTK packages into the chosen directory so runtime import doesn't try to write
123
+ ENV NLTK_DATA=/app/nltk_data
124
  RUN python -m nltk.downloader -d /app/nltk_data punkt averaged_perceptron_tagger wordnet || true
125
 
126
+ # Populate font cache (will quiet fontconfig warnings)
 
 
 
 
 
 
 
 
 
 
 
127
  RUN fc-cache -f -v || true
128
 
129
+ # Create a less-privileged user and give ownership of /app to that user
130
+ RUN useradd -m -u 1000 appuser \
131
+ && chown -R appuser:appuser /app
132
+
133
+ # Switch to non-root user (IMPORTANT: do this AFTER chown)
134
+ USER appuser
135
+
136
  # Set Flask env
137
  ENV FLASK_APP=app.py \
138
  FLASK_ENV=production
 
140
  # Expose port
141
  EXPOSE 7860
142
 
143
+ # HEALTHCHECK (optional) — uses lightweight endpoint; if you don't have /healthz, change it or remove.
144
+ HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
145
+ CMD curl -f http://127.0.0.1:7860/healthz || exit 1
146
 
147
+ # Run Gunicorn as non-root appuser: single worker + threads, no timeout
148
  CMD ["gunicorn", "app:app", "-b", "0.0.0.0:7860", "-w", "1", "--threads", "4", "-k", "gthread", "--timeout", "0", "--graceful-timeout", "0"]