-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile.backend
More file actions
99 lines (72 loc) · 2.75 KB
/
Dockerfile.backend
File metadata and controls
99 lines (72 loc) · 2.75 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
# Multi-stage build for Fastify backend with Python
# Stage 1: Dependencies
FROM node:20-alpine AS deps
WORKDIR /app
# Install build dependencies for native modules
RUN apk add --no-cache python3 make g++ gcc
# Install pnpm
RUN npm install -g pnpm@9.15.4
# Copy package files
COPY package.json pnpm-lock.yaml* ./
COPY packages/backend/package.json ./packages/backend/
# Install dependencies (including better-sqlite3)
RUN pnpm install --frozen-lockfile
# Stage 2: Python Environment
FROM python:3.12-alpine AS python-builder
WORKDIR /app
# Install Python dependencies
COPY packages/backend/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
# Stage 3: Builder
FROM node:20-alpine AS builder
WORKDIR /app
# Install build dependencies
RUN apk add --no-cache python3 make g++ gcc
# Install pnpm
RUN npm install -g pnpm@9.15.4
# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/packages/backend/node_modules ./packages/backend/node_modules
# Copy source code
COPY packages/backend ./packages/backend
# Build TypeScript
RUN cd packages/backend && pnpm build
# Stage 4: Runner
FROM python:3.12-alpine AS runner
WORKDIR /app
# Install Node.js
RUN apk add --no-cache nodejs npm
# Install pnpm
RUN npm install -g pnpm@9.15.4
# Create non-root user
RUN addgroup --system --gid 1001 backend
RUN adduser --system --uid 1001 backend
# Copy Python dependencies from python-builder
COPY --from=python-builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=python-builder /usr/local/bin /usr/local/bin
# Copy Node.js dependencies and built files
COPY --from=builder /app/packages/backend/node_modules ./packages/backend/node_modules
COPY --from=builder /app/packages/backend/dist ./packages/backend/dist
COPY --from=builder /app/packages/backend/package.json ./packages/backend/
# Copy database migrations
COPY packages/backend/src/db/migrations ./packages/backend/src/db/migrations
# Copy Python scripts
COPY python-scripts ./python-scripts
# Create necessary directories with correct permissions
RUN mkdir -p /app/.data/storage /app/.data/memory /app/.data/agents /app/.data/backups /app/public/storage
RUN chown -R backend:backend /app/.data /app/public/storage
# Set correct permissions
RUN chown -R backend:backend /app
# Switch to non-root user
USER backend
# Expose port
EXPOSE 41522
# Set environment variables
ENV NODE_ENV=production
ENV PORT=41522
ENV PYTHON_PATH=/usr/local/bin/python3
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD node -e "require('http').get('http://localhost:41522/api/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1))"
# Start Fastify server
CMD ["node", "packages/backend/dist/api/server.js"]