forked from realopsreactor/tech-stack-advisor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
69 lines (49 loc) · 1.76 KB
/
Dockerfile
File metadata and controls
69 lines (49 loc) · 1.76 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
# syntax=docker/dockerfile:1
# BuildKit optimized Dockerfile with advanced caching and multi-arch support
ARG TARGETPLATFORM
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH
# Stage 1: Dependencies stage with cache mounts
FROM --platform=$BUILDPLATFORM python:3.11-slim AS dependencies
WORKDIR /app
# Use cache mount for apt packages
RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/lib/apt \
apt-get update && apt-get install -y \
gcc \
python3-dev
# Use cache mount for pip packages
COPY requirements.txt .
RUN --mount=type=cache,target=/root/.cache/pip \
pip install --user -r requirements.txt
# Stage 2: Model training stage
FROM dependencies AS trainer
COPY train.py .
# Train the model with cache mount for any downloads
RUN --mount=type=cache,target=/tmp \
python train.py
# Stage 3: Final production stage
FROM --platform=$TARGETPLATFORM python:3.11-slim AS production
# Display build info for debugging
RUN echo "Building for platform: $TARGETPLATFORM, architecture: $TARGETARCH"
# Create non-root user
RUN useradd --create-home --shell /bin/bash mluser
WORKDIR /app
# Copy Python packages from dependencies stage
COPY --from=dependencies /root/.local /home/mluser/.local
# Copy application files
COPY app.py .
COPY requirements.txt .
# Copy trained models from trainer stage
COPY --from=trainer /app/*.pkl ./
# Set proper ownership
RUN chown -R mluser:mluser /app
USER mluser
# Update PATH for user-installed packages
ENV PATH=/home/mluser/.local/bin:$PATH
# Health check with improved reliability
HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860', timeout=5)" || exit 1
EXPOSE 7860
CMD ["python", "app.py"]