Dockerize client, server, and postgres database for production with container healthchecks

This commit is contained in:
2026-05-28 12:23:50 +02:00
parent 9a2052f623
commit 572d38e490
10 changed files with 176 additions and 11 deletions
+48
View File
@@ -0,0 +1,48 @@
# --- Build Stage ---
FROM node:20-alpine AS builder
WORKDIR /app
RUN apk add --no-cache openssl libc6-compat
# Copy package configurations
COPY package*.json ./
# Install all dependencies (including devDependencies for tsc)
RUN npm ci
# Copy Prisma schema and generate Client code
COPY prisma ./prisma
RUN npx prisma generate
# Copy source and compile TypeScript
COPY src ./src
COPY tsconfig.json ./
RUN npm run build
# --- Production Runner Stage ---
FROM node:20-alpine
WORKDIR /app
RUN apk add --no-cache openssl libc6-compat
# Copy package configurations
COPY package*.json ./
# Install only production dependencies
RUN npm ci --omit=dev
# Copy generated Prisma Client from builder stage
COPY --from=builder /app/node_modules/@prisma/client ./node_modules/@prisma/client
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
# Copy built code and runtime configs
COPY --from=builder /app/dist ./dist
COPY prisma ./prisma
# Expose backend PORT
EXPOSE 5000
# Health check utilizing native http module to query the backend health API (which queries PG database)
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
CMD node -e "const http = require('http'); http.get('http://localhost:5000/api/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1); }).on('error', () => process.exit(1));"
# Launch backend Express server
CMD ["node", "dist/index.js"]
+2 -2
View File
@@ -14,13 +14,13 @@
"@simplewebauthn/server": "^9.0.3",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2"
"express": "^4.19.2",
"prisma": "^5.10.2"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/node": "^20.11.24",
"prisma": "^5.10.2",
"tsx": "^4.7.1",
"typescript": "^5.3.3"
}
+19 -6
View File
@@ -4,6 +4,7 @@ import dotenv from 'dotenv'
import authRouter from './routes/auth.js'
import logbooksRouter from './routes/logbooks.js'
import syncRouter from './routes/sync.js'
import { prisma } from './db.js'
dotenv.config()
@@ -19,12 +20,24 @@ app.use('/api/logbooks', logbooksRouter)
app.use('/api/sync', syncRouter)
// Health check endpoint
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
timestamp: new Date().toISOString(),
service: 'Kapteins Daagbox Backend'
})
app.get('/api/health', async (req, res) => {
try {
await prisma.$queryRaw`SELECT 1`
res.json({
status: 'ok',
database: 'connected',
timestamp: new Date().toISOString(),
service: 'Kapteins Daagbox Backend'
})
} catch (err: any) {
res.status(500).json({
status: 'error',
database: 'disconnected',
error: err.message,
timestamp: new Date().toISOString(),
service: 'Kapteins Daagbox Backend'
})
}
})
app.listen(PORT, () => {