forked from ianarawjo/ChainForge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
84 lines (63 loc) · 2.79 KB
/
Dockerfile
File metadata and controls
84 lines (63 loc) · 2.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
# Multi-stage build: Stage 1 - Build React frontend
FROM node:20-slim AS frontend-builder
WORKDIR /app
# Copy package files and install dependencies (including dev for build)
COPY chainforge/react-server/package*.json ./
RUN npm ci --legacy-peer-deps --prefer-offline
# Copy source files and build
COPY chainforge/react-server/ ./
RUN npm run build
# Stage 2 - Build Python dependencies (CPU version with constraints)
FROM python:3.12-slim AS python-builder
# Install only the build dependencies we need
RUN apt-get --allow-releaseinfo-change update && \
apt-get install -y --no-install-recommends \
build-essential \
git \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /build
# Upgrade pip tools first (this layer is highly cacheable)
RUN pip install --no-cache-dir --upgrade pip setuptools wheel
# Copy requirements first for better layer caching
COPY chainforge/requirements.txt chainforge/constraints.txt ./
# Install PyTorch CPU-only FIRST as it's the largest dependency
# This separates the longest-running install into its own layer
RUN pip install --no-cache-dir --prefix=/install \
--extra-index-url https://download.pytorch.org/whl/cpu \
torch torchvision torchaudio
# Install remaining requirements with constraints
# Using --find-links to help pip resolve faster
RUN pip install --no-cache-dir --prefix=/install \
-r requirements.txt \
-c constraints.txt
# Copy project files and build the package (smallest layer last)
COPY setup.py README.md ./
COPY chainforge/ ./chainforge/
RUN pip install --no-cache-dir --prefix=/install .
# Stage 3 - Final minimal runtime image
FROM python:3.12-slim
# Install only runtime dependencies (no build tools)
RUN apt-get --allow-releaseinfo-change update && \
apt-get install -y --no-install-recommends \
git \
libgomp1 \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
WORKDIR /chainforge
# Copy Python packages from builder
COPY --from=python-builder /install /usr/local
# Copy the built React app from the frontend-builder stage to the installed package location
COPY --from=frontend-builder /app/build /usr/local/lib/python3.12/site-packages/chainforge/react-server/build
# Clean up any unnecessary files to reduce image size
RUN find /usr/local -type d -name "tests" -exec rm -rf {} + 2>/dev/null || true && \
find /usr/local -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true && \
find /usr/local -name "*.pyc" -delete && \
find /usr/local -name "*.pyo" -delete && \
find /usr/local -name "*.md" -delete 2>/dev/null || true
# Run as non-root user for security
RUN useradd -m -u 1000 chainforge && \
mkdir -p /home/chainforge/.local/share/chainforge && \
chown -R chainforge:chainforge /chainforge /home/chainforge
USER chainforge
EXPOSE 8000
ENTRYPOINT [ "chainforge", "serve", "--host", "0.0.0.0" ]