-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
111 lines (80 loc) · 2.51 KB
/
Dockerfile
File metadata and controls
111 lines (80 loc) · 2.51 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
# ---- Base Node ----
FROM node:20-alpine AS base
# Add maintainer info
LABEL maintainer="Manjericao <team.manjericao@gmail.com>"
# Set working directory
WORKDIR /app
# Add non-root user for security
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# Install dependencies required for node-gyp and health check
RUN apk add --no-cache python3 make g++ curl
# ---- Dependencies ----
FROM base AS dependencies
# Copy package files
COPY package*.json ./
COPY tsconfig*.json ./
# Install ALL dependencies
RUN npm ci
# Then copy production dependencies for later use
RUN cp -R node_modules /tmp/node_modules && \
npm ci --only=production && \
cp -R node_modules /tmp/prod_node_modules
# ---- Build ----
FROM dependencies AS builder
# Copy source
COPY . .
# Build application
RUN npm run build
# Remove development dependencies
RUN npm prune --production
# ---- Security Scanner ----
FROM aquasec/trivy:latest AS security-scanner
WORKDIR /scan
COPY --from=builder /app .
RUN trivy filesystem --no-progress --exit-code 1 --severity HIGH,CRITICAL --ignore-unfixed .
# ---- Release ----
FROM node:18-alpine AS release
# Set environment variables
ENV NODE_ENV=production
ENV PORT=3000
# Set working directory
WORKDIR /app
# Add non-root user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# Install production dependencies only
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package*.json ./
# Copy configuration files
COPY --from=builder /app/.env.example ./.env.example
COPY --from=builder /app/tsconfig*.json ./
# Set ownership to non-root user
RUN chown -R appuser:appgroup /app
# Switch to non-root user
USER appuser
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=30s \
CMD curl -f http://localhost:${PORT}/health || exit 1
# Expose port
EXPOSE ${PORT}
# Set Node.js memory limits
ENV NODE_OPTIONS="--max-old-space-size=2048"
# Define entry point
CMD ["node", "dist/index.js"]
# Add metadata
LABEL org.opencontainers.image.source="https://github.com/manjericao/ppl" \
org.opencontainers.image.description="Production-ready TypeScript Clean Architecture API" \
org.opencontainers.image.licenses="MIT"
# Security configurations
RUN apk add --no-cache dumb-init
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
# ---- Development ----
FROM dependencies AS development
# Set environment
ENV NODE_ENV=development
# Copy source
COPY . .
# Expose development port
EXPOSE 3000
# Start development server
CMD ["npm", "run", "dev"]