benkada commited on
Commit
6b5af3e
·
verified ·
1 Parent(s): 01c75f4

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +45 -6
Dockerfile CHANGED
@@ -1,16 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  FROM python:3.9-slim
2
 
3
- # Create and set cache directory
4
  ENV TRANSFORMERS_CACHE=/app/cache
5
- RUN mkdir -p ${TRANSFORMERS_CACHE} && chmod 777 ${TRANSFORMERS_CACHE}
 
 
 
 
 
 
 
6
 
7
  WORKDIR /app
8
- COPY requirements.txt .
9
 
10
- # Install packages
11
- RUN pip install --no-cache-dir -r requirements.txt
 
 
 
 
12
 
13
- COPY . .
 
14
 
 
 
 
 
 
 
15
  EXPOSE 8000
 
 
 
 
 
 
16
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
 
1
+ FROM python:3.9-slim AS builder
2
+
3
+ # Install system dependencies
4
+ RUN apt-get update && apt-get install -y --no-install-recommends \
5
+ build-essential \
6
+ && rm -rf /var/lib/apt/lists/*
7
+
8
+ # Set working directory
9
+ WORKDIR /app
10
+
11
+ # Copy requirements
12
+ COPY requirements.txt .
13
+
14
+ # Install Python dependencies
15
+ RUN pip install --no-cache-dir --user -r requirements.txt
16
+
17
  FROM python:3.9-slim
18
 
19
+ # Create and set cache directory for transformers
20
  ENV TRANSFORMERS_CACHE=/app/cache
21
+ ENV PYTHONDONTWRITEBYTECODE=1
22
+ ENV PYTHONUNBUFFERED=1
23
+
24
+ # Create app directory and cache directory
25
+ RUN mkdir -p /app ${TRANSFORMERS_CACHE} && \
26
+ # Create a non-root user to run the application
27
+ useradd -m appuser && \
28
+ chown -R appuser:appuser /app ${TRANSFORMERS_CACHE}
29
 
30
  WORKDIR /app
 
31
 
32
+ # Copy installed packages from builder
33
+ COPY --from=builder /root/.local /home/appuser/.local
34
+ ENV PATH=/home/appuser/.local/bin:$PATH
35
+
36
+ # Copy application code
37
+ COPY --chown=appuser:appuser . .
38
 
39
+ # Switch to non-root user
40
+ USER appuser
41
 
42
+ # Pre-download models to avoid cold starts in production
43
+ RUN python -c "from transformers import pipeline; \
44
+ pipeline('summarization', model='facebook/bart-large-cnn'); \
45
+ pipeline('image-to-text', model='nlpconnect/vit-gpt2-image-captioning')"
46
+
47
+ # Expose the port the app runs on
48
  EXPOSE 8000
49
+
50
+ # Health check
51
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
52
+ CMD curl -f http://localhost:8000/ || exit 1
53
+
54
+ # Command to run the application
55
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]