blob: 0efc8c5ecd628394edd85a69ec6dcf3e5a2ee3ea [file] [log] [blame]
Marc Kupietz86044852025-11-29 10:19:03 +01001# Multi-stage Docker build for size optimization
2FROM python:3.12-slim-bookworm AS builder
3
4# Install build dependencies
5RUN apt-get update && apt-get install -y \
6 gcc \
7 g++ \
8 && rm -rf /var/lib/apt/lists/*
9
10# Set environment variables
11ENV PIP_CACHE_DIR="/tmp/.cache/pip" \
12 PYTHONPATH="PYTHONPATH:."
13ENV VIRTUAL_ENV=/app/venv
14ENV PATH="$VIRTUAL_ENV/bin:$PATH"
15
16# Set the working directory and copy requirements
17WORKDIR /app
18COPY requirements.txt /app/requirements.txt
19
20# Install Python dependencies in virtual environment
21RUN python -m venv venv
22RUN venv/bin/pip install --upgrade pip
23RUN venv/bin/pip install -r requirements.txt
24
25# Production stage
26FROM python:3.12-slim-bookworm AS production
27
28# Install minimal runtime dependencies
29RUN apt-get update && apt-get install -y \
30 wget \
31 coreutils \
32 && rm -rf /var/lib/apt/lists/* \
33 && apt-get clean
34
Marc Kupietz9baa27a2025-11-29 15:32:16 +010035# Add non-root user FIRST (before copying files)
36RUN groupadd -r appuser && useradd -r -g appuser appuser
Marc Kupietz86044852025-11-29 10:19:03 +010037
Marc Kupietz9baa27a2025-11-29 15:32:16 +010038# Copy virtual environment from builder and set ownership immediately
39COPY --from=builder --chown=appuser:appuser /app/venv /app/venv
40
41# Copy application code with correct ownership
42COPY --chown=appuser:appuser lib /app/lib
43COPY --chown=appuser:appuser systems /app/systems
44COPY --chown=appuser:appuser my_utils /app/my_utils
45COPY --chown=appuser:appuser download_with_progress.py /app/download_with_progress.py
46COPY --chown=appuser:appuser list_spacy_models.py /app/list_spacy_models.py
47COPY --chown=appuser:appuser docker-entrypoint.sh /docker-entrypoint.sh
Marc Kupietz86044852025-11-29 10:19:03 +010048
49# Set environment variables
50ENV VIRTUAL_ENV=/app/venv
51ENV PATH="$VIRTUAL_ENV/bin:$PATH"
52ENV PYTHONPATH="PYTHONPATH:."
53
54# spaCy processing configuration
55ENV SPACY_USE_DEPENDENCIES="True"
56ENV SPACY_USE_GERMALEMMA="True"
57ENV SPACY_PARSE_TIMEOUT="30"
58ENV SPACY_MAX_SENTENCE_LENGTH="500"
59ENV SPACY_N_PROCESS="10"
60ENV SPACY_BATCH_SIZE="2000"
61ENV SPACY_CHUNK_SIZE="20000"
62
63WORKDIR /app
Marc Kupietz9baa27a2025-11-29 15:32:16 +010064
65# Create directories with correct ownership
66RUN mkdir -p "/app/logs" "/app/tmp" "/local/models" && \
67 chown -R appuser:appuser "/app/logs" "/app/tmp" "/local/models" && \
68 chmod +x /docker-entrypoint.sh && \
69 chmod +x /app/download_with_progress.py && \
70 chmod +x /app/list_spacy_models.py
Marc Kupietz86044852025-11-29 10:19:03 +010071
72# Set temp directories to use app directory instead of system /tmp
73ENV TMPDIR="/app/tmp"
74ENV TEMP="/app/tmp"
75ENV TMP="/app/tmp"
76
Marc Kupietzc75ae7c2025-11-29 10:41:26 +010077# Switch to non-root user
78USER appuser
Marc Kupietz86044852025-11-29 10:19:03 +010079
80# Define the entry point
81ENTRYPOINT ["/docker-entrypoint.sh"]