Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
79 lines (61 loc) · 1.99 KB
/
Copy pathDockerfile
File metadata and controls
79 lines (61 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Stage 1: Builder
FROM python:3.11-slim as builder
WORKDIR /app
# Install build dependencies
RUN apt-get update && apt-get install -y curl build-essential && rm -rf /var/lib/apt/lists/*
# Create virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Install dependencies
COPY requirements.txt .
ARG INSTALL_TYPE=cpu
RUN pip install --no-cache-dir --upgrade pip && \
if [ "$INSTALL_TYPE" = "cpu" ]; then \
echo "Installing CPU-only PyTorch"; \
pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu; \
else \
echo "Installing Standard PyTorch (CUDA)"; \
pip install --no-cache-dir torch; \
fi && \
pip install --no-cache-dir -r requirements.txt
# Pre-download models
# We need to copy the script and config to the builder stage
COPY models.yaml .
COPY download_models.py .
# Set HF_HOME to a specific location we can copy later
ENV HF_HOME=/app/hf_cache
RUN python download_models.py
# Stage 2: Runtime
FROM python:3.11-slim
WORKDIR /app
# Install runtime dependencies
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Create a non-root user
RUN useradd -m -u 1000 appuser
# Define Environment Variables
ENV HF_HOME=/home/appuser/.cache/huggingface
ENV LOG_LEVEL=INFO
ENV WORKERS=1
ENV AUTH_MODE=NONE
# Copy configuration and code
COPY models.yaml .
COPY main.py .
COPY app ./app
# Copy downloaded models from builder
# We copy them to the user's cache directory
COPY --from=builder /app/hf_cache /home/appuser/.cache/huggingface
# Change ownership
RUN chown -R appuser:appuser /app /home/appuser
# Switch to non-root user
USER appuser
# Expose ports (HTTP and gRPC)
EXPOSE 8000
EXPOSE 50051
# Healthcheck
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# Run the application
CMD ["python", "main.py"]