Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
93 lines (78 loc) · 3.79 KB
/
Copy pathDockerfile
File metadata and controls
93 lines (78 loc) · 3.79 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
# ==========================================================
# Crypto Quant Platform
# ==========================================================
# Two-stage build. TA-Lib is compiled once in the builder and the
# resulting wheels are copied into a slim runtime image, so the final
# image carries no compiler toolchain.
# ==========================================================
FROM python:3.10-slim-bookworm AS builder
ENV PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
wget \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# TA-Lib C library — required by the ta-lib Python package.
ARG TA_LIB_VERSION=0.6.4
RUN wget -q "https://github.com/ta-lib/ta-lib/releases/download/v${TA_LIB_VERSION}/ta-lib-${TA_LIB_VERSION}-src.tar.gz" \
&& tar -xzf "ta-lib-${TA_LIB_VERSION}-src.tar.gz" \
&& cd "ta-lib-${TA_LIB_VERSION}" \
&& ./configure --prefix=/usr \
&& make -j"$(nproc)" \
&& make install \
&& cd .. \
&& rm -rf "ta-lib-${TA_LIB_VERSION}" "ta-lib-${TA_LIB_VERSION}-src.tar.gz"
WORKDIR /build
# The lock, not requirements.txt. requirements.txt lists direct dependencies
# with floating transitives; two builds of the same commit could resolve
# different versions of the other 85 packages. The lock pins all 114 with
# hashes, so the image is byte-reproducible and --require-hashes rejects any
# artefact that does not match.
COPY requirements.lock .
RUN pip wheel --wheel-dir /wheels --require-hashes -r requirements.lock
# ----------------------------------------------------------
FROM python:3.10-slim-bookworm AS runtime
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=1 \
DATABASE_PATH=/app/database
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
# kaleido ships a bundled Chromium that renders chart PNGs; on the
# slim image these are the shared libraries it expects to find.
# Without libexpat1 it fails with "error while loading shared
# libraries: libexpat.so.1", and chart generation silently degrades
# to an empty image.
libexpat1 \
libnss3 \
libgconf-2-4 \
&& rm -rf /var/lib/apt/lists/*
# The TA-Lib shared objects the Python package links against.
COPY --from=builder /usr/lib/libta-lib* /usr/lib/
RUN ldconfig
COPY --from=builder /wheels /wheels
# Installs the wheel set the builder produced, not the lock. Hashes were
# already verified there, at download time, against requirements.lock.
# Re-checking here cannot work: ta-lib ships only as an sdist, so the
# builder compiles it, and a locally built wheel never matches the hash of
# the sdist it came from. --no-deps is safe because /wheels is the complete
# resolved closure, and --no-index means nothing is fetched from the network.
RUN pip install --no-index --no-deps /wheels/*.whl \
&& rm -rf /wheels
# Run as a non-root user: this process holds exchange credentials.
RUN useradd --create-home --shell /usr/sbin/nologin trader
WORKDIR /app
COPY --chown=trader:trader . .
RUN mkdir -p /app/database /app/logs && chown -R trader:trader /app
USER trader
EXPOSE 5000
# Generous timeout and retries on purpose: backtests are CPU-bound and run
# synchronously on the gevent worker, so a long request can delay the health
# response. Marking the container unhealthy mid-backtest would be a false
# alarm. See docs/ROADMAP.md — offloading CPU work to a pool is the real fix.
HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=5 \
CMD curl -fsS http://localhost:5000/api/health || exit 1
# Plain HTTP by default: terminate TLS at your reverse proxy or load
# balancer. Pass --certfile/--keyfile to serve HTTPS directly.
ENTRYPOINT ["python", "-u", "-m", "core.webapp.app"]