forked from dapperdivers/developer-portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
95 lines (71 loc) · 2.34 KB
/
Dockerfile
File metadata and controls
95 lines (71 loc) · 2.34 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
# Stage 1: Build
FROM node:22-slim AS builder
# Set working directory
WORKDIR /app
# Set build-time environment variables
ARG PORT=3001
ARG ALLOWED_DOMAINS=http://localhost:${PORT}
ARG VITE_SITE_MAIN_URL
ARG VITE_SITE_STORYBOOK_URL
ARG VITE_SITE_DOCS_URL
ARG VITE_SITE_NAVIGATOR_ENABLED=true
ENV NODE_ENV=production
ENV PORT=${PORT}
ENV ALLOWED_DOMAINS=${ALLOWED_DOMAINS}
ENV REACT_APP_PORT=${PORT}
ENV REACT_APP_NODE_ENV=${NODE_ENV}
ENV GENERATE_SOURCEMAP=false
# Site Navigator environment variables
ENV VITE_SITE_MAIN_URL=${VITE_SITE_MAIN_URL}
ENV VITE_SITE_STORYBOOK_URL=${VITE_SITE_STORYBOOK_URL}
ENV VITE_SITE_DOCS_URL=${VITE_SITE_DOCS_URL}
ENV VITE_SITE_NAVIGATOR_ENABLED=${VITE_SITE_NAVIGATOR_ENABLED}
# Install dependencies first (better layer caching)
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --production=false --network-timeout 600000
# Copy source code
COPY . .
# Build the site navigator with environment variables
RUN yarn build:site-navigator
# Build the application
RUN yarn build
# Clean up node_modules to start fresh
RUN rm -rf node_modules
# Stage 2: Dependencies
FROM node:22-alpine AS deps
WORKDIR /app
# Copy package files
COPY --from=builder /app/package.json /app/yarn.lock ./
# Install only production dependencies
RUN yarn install --production --frozen-lockfile --network-timeout 600000 \
&& yarn cache clean
# Stage 3: Production
FROM node:22-alpine AS production
# Set runtime environment variables with defaults
ARG PORT=3001
ARG ALLOWED_DOMAINS=http://localhost:3001
ENV PORT=${PORT}
ENV ALLOWED_DOMAINS=${ALLOWED_DOMAINS}
ENV NODE_OPTIONS="--max-old-space-size=512"
ENV NODE_ENV=production
# Create non-root user
RUN addgroup -S nodeapp && adduser -S -G nodeapp nodeapp
WORKDIR /app
# Install only curl for healthcheck
RUN apk --no-cache add curl
# Copy built assets and production dependencies
COPY --from=builder /app/build ./build
COPY --from=builder /app/files ./files
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app/server.js ./server.js
# Change ownership
RUN chown -R nodeapp:nodeapp /app
# Add healthcheck
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:${PORT}/healthz || exit 1
# Expose port (IPv4 only)
EXPOSE ${PORT}/tcp
# Switch to non-root user
USER nodeapp
# Start secure express server
CMD ["node", "server.js"]