This repository was archived by the owner on Feb 11, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
84 lines (64 loc) · 2.39 KB
/
Copy pathDockerfile
File metadata and controls
84 lines (64 loc) · 2.39 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
# --- STAGE 1: Build stage ---
FROM node:22-alpine3.21 AS builder
WORKDIR /app
# Update Alpine packages to latest security patches
RUN apk update && apk upgrade --no-cache
# Kopieer package files en config
COPY package*.json ./
COPY prisma.config.ts ./
# Install dependencies
RUN npm ci
# Kopieer broncode en tsconfig
COPY tsconfig*.json nest-cli.json ./
COPY src ./src
# Genereer Prisma client (met dummy URL voor build-tijd)
ENV DATABASE_URL="mysql://user:password@localhost:3306/db"
RUN npm run db:generate
# Bouw de NestJS applicatie
RUN npm run build
# --- STAGE 2: Production stage ---
FROM node:22-alpine3.21 AS production
WORKDIR /app
# Update Alpine packages to latest security patches and create non-root user
RUN apk update && apk upgrade --no-cache && \
addgroup -g 1001 -S nodejs && \
adduser -S nestjs -u 1001
# Kopieer package files en installeer alleen production dependencies
COPY package*.json ./
RUN npm ci --only=production && npm cache clean --force
# Kopieer de gebouwde applicatie en Prisma bestanden van builder
COPY --from=builder /app/src/infrastructure/database/generated ./src/infrastructure/database/generated
COPY --from=builder /app/src/infrastructure/database/schema ./src/infrastructure/database/schema
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/prisma.config.ts ./
# Maak het start-script aan (gecorrigeerd voor shell-fouten en paden)
RUN <<EOF cat > /app/start.sh
#!/bin/sh
set -e
echo "Starting application..."
# Run database migrations
echo "Running database migrations..."
npx prisma migrate deploy
# Check of main.js in dist of dist/src staat
if [ -f "dist/main.js" ]; then
node dist/main.js
elif [ -f "dist/src/main.js" ]; then
node dist/src/main.js
else
echo "ERROR: Could not find main.js in dist/ or dist/src/"
ls -R dist/
exit 1
fi
EOF
# Maak script uitvoerbaar en zet de eigenaar op de nestjs user
RUN chmod +x /app/start.sh && \
chown -R nestjs:nodejs /app
# Wissel naar de veilige user
USER nestjs
# Expose poort (standaard NestJS poort)
EXPOSE 4000
# Gebruik ENTRYPOINT om het script te starten
ENTRYPOINT ["/bin/sh", "/app/start.sh"]
# Gezondheidscontrole voor Azure (correcte poort 4000)
HEALTHCHECK --interval=30s --timeout=3s --start-period=30s --retries=3 \
CMD node -e "require('http').get('http://localhost:4000/api/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1);}).on('error', (e) => {process.exit(1);});"