-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
128 lines (93 loc) · 3.92 KB
/
Dockerfile
File metadata and controls
128 lines (93 loc) · 3.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Stage 1: Build Frontend
FROM oven/bun:latest AS frontend-builder
WORKDIR /app
# Setup directories to match the structure expected by build scripts
RUN mkdir -p frontend zig
# Install Frontend Dependencies
WORKDIR /app/frontend
COPY frontend/package.json frontend/bun.lock* ./
RUN bun install
# Copy Frontend Source
COPY frontend/ ./
# Build Frontend
# This runs "vite build" with BUILD_TO_ZIG=1.
# vite.config.ts outputs to ../zig/frontend (which is /app/zig/frontend)
RUN bun run build:zig
# Get Zig compiler toolchain from reliable community image
FROM kassany/alpine-ziglang:0.15.1 AS zig-toolchain
# Stage 2: Build Backend (Zig)
FROM debian:bookworm-slim AS backend-builder
WORKDIR /app
# Install system dependencies required for zig build/fetch/linking
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
libsqlite3-dev \
git \
ca-certificates \
curl \
&& rm -rf /var/lib/apt/lists/*
# Copy Zig toolchain from the actual locations in kassany/alpine-ziglang:0.15.1
# Binary + lib dir are under /zig/0.15.1/files/
COPY --from=zig-toolchain /zig/0.15.1/files/zig /usr/local/bin/zig
COPY --from=zig-toolchain /zig/0.15.1/files/lib /usr/local/lib/zig/
# Optional: if you get linker errors later (missing compiler-rt, libc++, etc.), add these
# COPY --from=zig-toolchain /zig/0.15.1/files/lib/libc /usr/local/lib/libc/ || true
# COPY --from=zig-toolchain /zig/0.15.1/files/lib/libcxx /usr/local/lib/libcxx/ || true
# COPY --from=zig-toolchain /zig/0.15.1/files/lib/compiler-rt /usr/local/lib/compiler-rt/ || true
# Add zig to PATH
ENV PATH="/usr/local/bin:${PATH}"
# Debug: uncomment during troubleshooting to confirm Zig works and paths are correct
# RUN zig version || echo "Zig binary not found or not executable" && \
# ls -la /usr/local/bin/zig && \
# ls -la /usr/local/lib/zig || echo "Lib dir missing"
# Copy Frontend Build from Stage 1 for embedding
COPY --from=frontend-builder /app/zig/frontend /app/frontend/build
# Copy Your Zig source code
COPY zig/ ./zig/
# Build the Zig binary
WORKDIR /app/zig
# Fetch deps + build (add -v for verbose output if debugging)
# Also ensure libfacil.io.so is copied to a known location
RUN zig build -Doptimize=ReleaseSafe 2>&1 | tee /tmp/build.log || true
RUN mkdir -p /tmp/lib && find /root/.cache/zig/p -name "libfacil.io.so" -exec cp {} /tmp/lib/ \; 2>/dev/null || true
RUN ls -la /tmp/lib/ 2>/dev/null || echo "Library not in cache"
# Stage 3: Final Runtime
FROM debian:bookworm-slim AS runtime
WORKDIR /app
# Install minimal runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
libsqlite3-0 \
ca-certificates \
curl \
&& rm -rf /var/lib/apt/lists/*
# Create persistent data directories
RUN mkdir -p /data/git-repos /app/frontend /app/drizzle
# Copy built backend binary
COPY --from=backend-builder /app/zig/zig-out/bin/selfhost-server /app/selfhost-server
# Copy libfacil.io.so from build cache if available
COPY --from=backend-builder /tmp/lib/libfacil.io.so /usr/local/lib/libfacil.io.so 2>/dev/null || true
RUN ldconfig 2>/dev/null || true
# Copy and setup entrypoint script for library loading
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Frontend assets are now embedded in the binary.
# COPY --from=frontend-builder /app/zig/frontend /app/frontend
# Copy drizzle migrations from build context (source files)
COPY drizzle/ /app/drizzle/
# Set permissions for non-root user
RUN chown -R www-data:www-data /data /app
# Environment variables
ENV DATABASE_URL=file:/data/sqlite.db \
LOGGING_DATABASE_URL=file:/data/sqlite-logs.db \
STATIC_DIR=/app/frontend \
DRIZZLE_DIR=/app/drizzle \
PORT=3000 \
GIT_REPOS_ROOT=/data/git-repos
# Persistent volume for sqlite DBs + git repos
VOLUME ["/data"]
EXPOSE 3000
# Run as non-root user
USER www-data
# Start the server with entrypoint script
ENTRYPOINT ["/entrypoint.sh"]
CMD ["/app/selfhost-server"]