-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
105 lines (88 loc) · 3.46 KB
/
Copy pathDockerfile
File metadata and controls
105 lines (88 loc) · 3.46 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# syntax=docker/dockerfile:1.6
# ============================================================================
# StockLens AI — production container image.
#
# Two-stage build:
# 1) ``builder`` — installs build-time deps (gcc, headers) and resolves all
# Python wheels into a private cache.
# 2) ``runtime`` — slim image that only carries the resolved site-packages
# and the application source. No compilers, no caches.
#
# Build:
# docker build -t stocklens:latest .
#
# Run (interactive):
# docker run --rm -it \
# -p 8000:8000 \
# -v $(pwd)/data:/app/data \
# -v $(pwd)/logs:/app/logs \
# --env-file .env \
# stocklens:latest
#
# Run via compose:
# docker compose up -d # see docker-compose.yml
# ============================================================================
# ----------------------------------------------------------------------------
# Stage 1: builder
# ----------------------------------------------------------------------------
FROM python:3.11-slim-bookworm AS builder
# Build-time tooling — needed for any wheel that has C extensions
# (numpy/pandas/lxml/zstandard ship wheels for x86_64 + arm64 on PyPI,
# so we don't usually compile, but gcc keeps the option open for
# unusual platforms).
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential \
gcc \
libffi-dev \
libssl-dev \
curl \
&& rm -rf /var/lib/apt/lists/*
ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1 \
PYTHONDONTWRITEBYTECODE=1
WORKDIR /build
# Copy only the requirements first so changes to source don't bust the
# (much heavier) dependency layer.
COPY requirements.txt ./
RUN python -m pip install --upgrade pip \
&& pip install --prefix=/install -r requirements.txt
# ----------------------------------------------------------------------------
# Stage 2: runtime
# ----------------------------------------------------------------------------
FROM python:3.11-slim-bookworm AS runtime
# Runtime libs only — no compilers.
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
curl \
tini \
&& rm -rf /var/lib/apt/lists/*
# Drop privileges. Application data lives under the user's HOME so
# bind-mounted volumes can carry the right ownership.
RUN groupadd --gid 1000 stocklens \
&& useradd --uid 1000 --gid stocklens --create-home --shell /bin/bash stocklens
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PYTHONPATH=/app \
LOG_DIR=/app/logs \
DATABASE_PATH=/app/data/stock_analysis.db \
WEBUI_HOST=0.0.0.0 \
WEBUI_PORT=8000
# Copy installed packages from the builder stage.
COPY --from=builder /install /usr/local
WORKDIR /app
# Copy source. .dockerignore excludes data/, logs/, tests, .venv etc.
COPY --chown=stocklens:stocklens . /app
# Pre-create writable dirs that the container will mount or write to.
RUN mkdir -p /app/data /app/logs /app/data/kvcache \
&& chown -R stocklens:stocklens /app/data /app/logs
USER stocklens
EXPOSE 8000
# Healthcheck hits the FastAPI app's lightweight ping.
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
CMD curl --fail --silent --max-time 4 http://127.0.0.1:8000/api/health || exit 1
# tini is PID 1 — proper signal forwarding for graceful shutdown.
ENTRYPOINT ["tini", "--"]
CMD ["python", "main.py"]