-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
109 lines (82 loc) · 3.38 KB
/
Dockerfile
File metadata and controls
109 lines (82 loc) · 3.38 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
# ---- Stage 1: Build Frontend ----
# Use a Node.js image to build the React app
FROM node:20-alpine AS frontend-builder
# Set working directory
WORKDIR /app/frontend
# Copy package.json and package-lock.json
COPY frontend/package*.json ./
# Install dependencies
RUN --mount=type=cache,target=/root/.npm npm ci
# Copy the rest of the frontend source code
COPY frontend/ ./
# Build the frontend for production
# These build args will be used as environment variables during build
ARG VITE_BACKEND_URL=/
ARG VITE_BACKEND_WS_URL=/
ARG VITE_APP_COMMIT
ARG VITE_APP_BUILD_TIME
ENV VITE_BACKEND_URL=$VITE_BACKEND_URL
ENV VITE_BACKEND_WS_URL=$VITE_BACKEND_WS_URL
ENV VITE_APP_COMMIT=$VITE_APP_COMMIT
ENV VITE_APP_BUILD_TIME=$VITE_APP_BUILD_TIME
# Workaround NPM optional dependency bug with Rollup native binaries when cross-compiling via buildx/QEMU.
# Explicitly install the correct platform-specific Rollup binary for Alpine (musl) based on TARGETPLATFORM.
ARG TARGETPLATFORM
RUN echo "Building for: ${TARGETPLATFORM}" && \
if [ "${TARGETPLATFORM}" = "linux/arm64" ]; then \
npm i -D @rollup/rollup-linux-arm64-musl; \
elif [ "${TARGETPLATFORM}" = "linux/amd64" ]; then \
npm i -D @rollup/rollup-linux-x64-musl; \
else \
echo "Unknown TARGETPLATFORM=${TARGETPLATFORM}, skipping explicit rollup native install"; \
fi
RUN --mount=type=cache,target=/root/.npm npm run build
# ---- Stage 2: Build Backend ----
# Use a Go image to build the backend app
FROM golang:1.24-alpine AS backend-builder
# Install build dependencies
RUN apk add --no-cache git
# Set working directory
WORKDIR /app
# Copy go.mod and go.sum
COPY backend/go.mod backend/go.sum ./
# Copy the rest of the backend source code
COPY backend/ ./
# Ensure module graph and sums are up to date after full copy
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go mod tidy && go mod download
# Build the backend executable
# CGO_ENABLED=0 is important for creating a static binary
# -o /app/server builds the executable and places it in /app/server
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build -buildvcs=false -trimpath -ldflags="-s -w" -o /app/server ./cmd/web/main.go
# ---- Stage 3: Final Production Image ----
# Use a lightweight base image like Alpine Linux
FROM alpine:latest
# Install ca-certificates for HTTPS support
RUN apk --no-cache add ca-certificates
# Set working directory
WORKDIR /app
# Create a non-root user to run the application
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# Copy the built backend executable from the backend-builder stage
COPY --from=backend-builder /app/server .
# Copy the built frontend static files from the builder stage
# We'll place them in a 'public' directory to be served by the backend
COPY --from=frontend-builder /app/frontend/dist ./public
# Copy the .env.example file for reference (optional)
COPY backend/.env.example .
# Change ownership of the app directory to appuser
RUN chown -R appuser:appgroup /app
# Switch to non-root user
USER appuser
# Expose the port the application will run on
EXPOSE 8080
# The command to run the application
# The backend will need to be modified to serve the static files from the './public' directory
ENV RAG_DB_PATH=/app/data/rag.db
RUN mkdir -p /app/data
VOLUME ["/app/data"]
CMD ["./server"]