-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
83 lines (61 loc) · 1.94 KB
/
Copy pathDockerfile
File metadata and controls
83 lines (61 loc) · 1.94 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
80
81
82
83
# Stage 1: Build stage
FROM python:3.11-slim-bookworm AS builder
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
# Install build dependencies
RUN apt-get update && \
apt-get install -y --no-install-recommends wget && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Set work directory
WORKDIR /build
# Download YOLO face detection model
RUN mkdir -p models && \
wget https://github.com/akanametov/yolo-face/releases/download/v0.0.0/yolov11n-face.pt -O models/yolov11n-face.pt
# Copy requirements and install them (needed for model download script)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy model download script
COPY download_model.py .
# Download and cache NSFW detection model
RUN python download_model.py
# Stage 2: Runtime stage
FROM python:3.11-slim-bookworm
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
# Install runtime dependencies only
RUN apt-get update && \
apt-get install -y --no-install-recommends \
libgl1-mesa-glx \
libglib2.0-0 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Create non-root user
RUN useradd -m appuser
# Set work directory
WORKDIR /app
# Copy requirements file
COPY requirements.txt .
# Install dependencies in virtual environment
RUN python -m venv /opt/venv && \
/opt/venv/bin/pip install --no-cache-dir -r requirements.txt
# Copy YOLO model from builder
COPY --from=builder /build/models /app/models
# Copy NSFW model cache from builder
COPY --from=builder /build/model-cache /app/model-cache
# Copy application code
COPY . .
# Set ownership
RUN chown -R appuser:appuser /app
# Set PATH to use virtual environment
ENV PATH="/opt/venv/bin:$PATH"
# Switch to non-root user
USER appuser
# Expose port
EXPOSE 8000
# Run the application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]