From 169e018335e91e2e025f82ac9d959245d93a4d4f Mon Sep 17 00:00:00 2001 From: A Nomad Date: Thu, 29 Jan 2026 18:11:28 +0100 Subject: [PATCH] Reorganize Dockerfile. Add the PM2 package for production runtime. Change the production runtime command --- Dockerfile | 45 ++++++++++++++++++++------------------- website/package-lock.json | 10 +++++++++ website/package.json | 3 ++- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3fb3b075..b12841ce 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,38 +1,39 @@ -FROM node:23-alpine +FROM node:23-alpine AS build +# Set build arguments with defaults +ARG NODE_ENV=production +ARG NPM_INSTALL_FLAGS= -# Create app directory and set permissions +# Set working directory WORKDIR /app -# Install dependencies needed for health checks with pinned version - RUN apk add --no-cache wget=1.25.0-r1 - - # Create a non-root user and group - RUN addgroup -S appgroup && adduser -S appuser -G appgroup +# Copy package files +COPY website/package*.json ./ + +# Install dependencies based on environment and flags +RUN npm ci $NPM_INSTALL_FLAGS -# Copy package files for efficient caching +# Copy website files COPY website/ ./ -# Set build arguments with defaults -ARG NODE_ENV=production -ARG NPM_INSTALL_FLAGS= +FROM node:23-alpine -# Install dependencies based on environment and flags -RUN npm ci $NPM_INSTALL_FLAGS && \ -# Clean npm cache to reduce image size -npm cache clean --force +# Set working directory +WORKDIR /app + +# Create a non-root user and group +RUN addgroup -S appgroup && adduser -S appuser -G appgroup +# Install dependencies needed for health checks with pinned version +RUN apk add --no-cache wget=1.25.0-r1 -# Set proper ownership -RUN chown -R appuser:appgroup /app +# Copy layer wfrom build image +COPY --chown=appuser:appgroup --from=build /app /app # Switch to non-root user USER appuser -# Define a health check -HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 CMD wget --no-verbose --tries=1 --spider http://localhost:3000/ || exit 1 - # Expose the port the app runs on EXPOSE 3000 -# Command to run the application -CMD ["npm", "start"] +# # Command to run the application +CMD ["npm", "start"] \ No newline at end of file diff --git a/website/package-lock.json b/website/package-lock.json index 2cb08cac..77e140fa 100644 --- a/website/package-lock.json +++ b/website/package-lock.json @@ -26,6 +26,7 @@ "mongodb": "^6.17.0", "node-fetch": "^2.6.7", "normalize.css": "^8.0.1", + "pm2-runtime": "^5.4.1", "postmark": "^4.0.5", "swiper": "^11.2.6", "yargs": "^17.7.2", @@ -14057,6 +14058,15 @@ "node": ">=8" } }, + "node_modules/pm2-runtime": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/pm2-runtime/-/pm2-runtime-5.4.1.tgz", + "integrity": "sha512-ywzOFlwf1KJHRRq2aqHEid/Qy3YptDlkOAo5dTC1oBo4H5OyDK7CAk3cBMN1AjOdKRSHkHP5DrmWIOeRc7IHPw==", + "license": "UNLICENSED", + "bin": { + "pm2-runtime": "wrapper.sh" + } + }, "node_modules/possible-typed-array-names": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.1.0.tgz", diff --git a/website/package.json b/website/package.json index 89e6fcb3..07694c7b 100644 --- a/website/package.json +++ b/website/package.json @@ -4,7 +4,7 @@ "description": "Apostrophe CMS website", "main": "app.js", "scripts": { - "start": "node scripts/generate_constants.js && node app @apostrophecms/asset:build && node app.js", + "start": "node scripts/generate_constants.js && node app @apostrophecms/asset:build && pm2-runtime app.js -i max", "dev": "node scripts/generate_constants.js && nodemon", "build": "node scripts/generate_constants.js && NODE_ENV=production node app.js apostrophe:generation", "serve": "NODE_ENV=production node app.js", @@ -55,6 +55,7 @@ "mongodb": "^6.17.0", "node-fetch": "^2.6.7", "normalize.css": "^8.0.1", + "pm2-runtime": "^5.4.1", "postmark": "^4.0.5", "swiper": "^11.2.6", "yargs": "^17.7.2",