Refactor Dockerfile to improve build stages and enhance clarity
taylorwalton committed
Nov 30, 2025 at 10:19 UTC
71768fed5b5c74bcfed21a592a714e273daf62ef
1 file changed
+43
-19
customer_portal/Dockerfile
+43
-19
@@ -1,38 +1,62 @@
1
-# Build stage
2
-FROM node:21 AS builder
1
+# Use a node.js base image
2
+FROM node:21 as builder
3
4
# Install pnpm globally
5
RUN npm install -g pnpm
6
7
+# Drop root
8
+USER node
9
+
10
# Set the working directory
11
WORKDIR /app
12
10
-# Copy package files
11
-COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
13
+# Copy the package.json and pnpm-lock.yaml files into the working directory
14
+COPY package.json pnpm-lock.yaml ./
15
13
-# Install project dependencies
14
-RUN pnpm install --frozen-lockfile
16
+# Install project dependencies using pnpm
17
+RUN pnpm install
18
16
-# Copy all source files
17
-COPY . .
19
+# Copy project files into the working directory
20
+COPY --chown=node . .
21
19
-# Build the application
22
+# Run the Vue.js project build using pnpm
23
RUN pnpm run build:only
24
22
-# Production stage
25
+FROM node:21 as dev
26
+
27
+RUN apt-get update && apt-get install -y openssl
28
+
29
+RUN mkdir -p /certs & \
30
+ openssl req -x509 -subj "/CN=localhost" -nodes -newkey rsa:4096 -keyout /certs/key.pem -out /certs/cert.pem -days 365 && \
31
+ chown -R node:node /certs
32
+
33
+# Drop root
34
+USER node
35
+
36
+# Set the working directory
37
+WORKDIR /app
38
+
39
+CMD pnpm install && pnpm run start-frontend
40
+
41
FROM nginx:1.24.0-alpine
42
25
-# Copy custom nginx config
26
-COPY nginx.conf /etc/nginx/nginx.conf
43
+# Added to create self-signed cert on run time if needed
44
+RUN apk add openssl
45
+
46
+# Set environment variables
47
+ENV SERVER_HOST=localhost
48
+ENV TLS_CERT_PATH=/etc/nginx/certs.d/server.crt
49
+ENV TLS_KEY_PATH=/etc/nginx/certs.d/server.key
50
28
-# Copy built static files from builder stage
29
-COPY --from=builder /app/dist /usr/share/nginx/html
51
+# Copy entrypoint
52
+COPY build/docker-entrypoint.d/90-copilot-ssl.sh /docker-entrypoint.d/90-copilot-ssl.sh
53
+RUN chmod +x /docker-entrypoint.d/90-copilot-ssl.sh
54
31
-# Expose port 80
32
-EXPOSE 80
55
+# Setup template
56
+RUN mkdir /etc/nginx/templates
57
+COPY build/etc/nginx/sites-enabled/default.conf /etc/nginx/templates/default.conf.template
58
34
-# Health check
35
-HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
36
- CMD wget --no-verbose --tries=1 --spider http://localhost/ || exit 1
59
+# Copy built static files from the builder stage
60
+COPY --from=builder /app/dist /var/www/copilot
61
62
CMD ["nginx", "-g", "daemon off;"]