Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
69 lines (52 loc) · 1.92 KB
/
Copy pathDockerfile
File metadata and controls
69 lines (52 loc) · 1.92 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
# ====================
# Stage 1: Builder
# ====================
FROM python:3.12-slim-bookworm AS builder
WORKDIR /app
# Install build dependencies
RUN apt-get update -y && \
apt-get install -y --no-install-recommends gcc g++ && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
# Copy dependency files for layer caching
COPY pyproject.toml uv.lock /app/
# Create a venv for the app dependencies
RUN uv venv /opt/venv
# Export production deps and install into the venv
RUN uv export --frozen --no-hashes --no-emit-project \
--no-group dev --no-group docs --no-group notebooks \
> requirements.txt && \
uv pip install --python /opt/venv/bin/python --no-cache -r requirements.txt
# ====================
# Stage 2: Runtime
# ====================
FROM python:3.12-slim-bookworm
WORKDIR /app
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONPATH="/app/src" \
PORT=5000
# Minimal runtime deps (libgomp1 required by XGBoost/CatBoost)
RUN apt-get update && apt-get install -y --no-install-recommends \
libgomp1 \
ca-certificates \
curl \
&& rm -rf /var/lib/apt/lists/*
# Copy the venv from builder
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Copy app code and model artifacts
COPY src/ ./src/
COPY saved_models/pipeline/ ./saved_models/pipeline/
# Prepare writable directories expected by the app
RUN mkdir -p data/raw data/processed data/external logs
# Non-root user
RUN useradd -m -u 1000 appuser && chown -R appuser /app
USER appuser
EXPOSE ${PORT}
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:5000/health')" || exit 1
# Use gunicorn for production WSGI serving
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "2", "--timeout", "120", "titanic_ml.app.routes:app"]