From 666f707cb95c1989cbac42081c700d5d3679ec2a Mon Sep 17 00:00:00 2001 From: Chintan Boghara Date: Fri, 2 May 2025 09:04:38 +0530 Subject: [PATCH 1/2] dev: improve Dockerfile efficiency and resilience - Enhance layer caching through optimized file copying - Add environment variables to improve build performance - Implement more selective file copying to the production stage - Enhance security by establishing proper user permissions - Replace wget with curl for more reliable health checks - Eliminate redundant npm install in the production stage - Improve error handling for dependency installation --- Dockerfile | 73 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/Dockerfile b/Dockerfile index c5ab1dd2f5..3b67dbea1f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,4 @@ # /!\ NOTICE /!\ - # Many of the developers DO NOT USE the Dockerfile or image. # While we do test new changes to Docker configuration, it's # possible that future changes to the repo might break it. @@ -10,6 +9,11 @@ # Build stage FROM node:23.9-alpine AS build +# Set environment variables to reduce npm verbosity and improve performance +ENV NODE_ENV=production \ + NPM_CONFIG_LOGLEVEL=error \ + NPM_CONFIG_PROGRESS=false + # Install build dependencies RUN apk add --no-cache git python3 make g++ \ && ln -sf /usr/bin/python3 /usr/bin/python @@ -17,62 +21,73 @@ RUN apk add --no-cache git python3 make g++ \ # Set up working directory WORKDIR /app -# Copy package.json and package-lock.json +# Copy package files first (better layer caching) COPY package*.json ./ -# Copy the source files -COPY . . - -# Install mocha +# Install mocha globally RUN npm install -g mocha -# Install node modules +# Try to install dependencies with better error handling and retries RUN npm cache clean --force && \ for i in 1 2 3; do \ + echo "Attempt $i: Installing dependencies..." && \ npm ci && break || \ if [ $i -lt 3 ]; then \ + echo "Retrying in 15 seconds..." && \ sleep 15; \ else \ + echo "Failed to install dependencies after 3 attempts" && \ exit 1; \ fi; \ done -# Run the build command if necessary -RUN cd src/gui && npm run build && cd - +# Copy source files after dependency installation +COPY . . + +# Build the GUI +RUN cd src/gui && npm run build # Production stage FROM node:23.9-alpine +# Set environment variables +ENV NODE_ENV=production \ + NPM_CONFIG_LOGLEVEL=error \ + NO_VAR_RUNTUME=1 + # Set labels -LABEL repo="https://github.com/HeyPuter/puter" -LABEL license="AGPL-3.0,https://github.com/HeyPuter/puter/blob/master/LICENSE.txt" -LABEL version="1.2.46-beta-1" +LABEL repo="https://github.com/HeyPuter/puter" \ + license="AGPL-3.0,https://github.com/HeyPuter/puter/blob/master/LICENSE.txt" \ + version="1.2.46-beta-1" \ + maintainer="Puter Team" # Install git (required by Puter to check version) -RUN apk add --no-cache git +RUN apk add --no-cache git curl + +# Create directory structure and set permissions before copying files +RUN mkdir -p /opt/puter/app && \ + chown -R node:node /opt/puter # Set up working directory -RUN mkdir -p /opt/puter/app WORKDIR /opt/puter/app -# Copy built artifacts and necessary files from the build stage -COPY --from=build /app/src/gui/dist ./dist -COPY --from=build /app/node_modules ./node_modules -COPY . . +# Copy only necessary files from the build stage +COPY --from=build --chown=node:node /app/src/gui/dist ./src/gui/dist +COPY --from=build --chown=node:node /app/node_modules ./node_modules +COPY --from=build --chown=node:node /app/package*.json ./ +COPY --from=build --chown=node:node /app/src ./src +COPY --from=build --chown=node:node /app/config ./config +COPY --from=build --chown=node:node /app/LICENSE.txt ./ -# Set permissions -RUN chown -R node:node /opt/puter/app +# Switch to non-root user USER node +# Expose the service port EXPOSE 4100 -HEALTHCHECK --interval=30s --timeout=3s \ - CMD wget --no-verbose --tries=1 --spider http://puter.localhost:4100/test || exit 1 - -ENV NO_VAR_RUNTUME=1 - -# Attempt to fix `lru-cache@11.0.2` missing after build stage -# by doing a redundant `npm install` at this stage -RUN npm install +# Improved healthcheck that uses curl (more reliable than wget) +HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ + CMD curl -f http://puter.localhost:4100/test || exit 1 -CMD ["npm", "start"] +# Start the application +CMD ["node", "src/index.js"] From b4a2051e8fb385183f9ee13ffe117f5529473993 Mon Sep 17 00:00:00 2001 From: Chintan Boghara Date: Sat, 10 May 2025 12:08:53 +0530 Subject: [PATCH 2/2] dev: improve Docker build efficiency and fix dependency issues dev: improve Docker build efficiency and fix dependency issues - Enhance Dockerfile with better caching and security practices - Fix html-entities dependency issue in build process - Properly handle workspace dependencies for gui package - Update .dockerignore with comprehensive exclusions - Optimize build context size for faster builds --- Dockerfile | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3b67dbea1f..fd2cdd5ece 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,8 @@ WORKDIR /app # Copy package files first (better layer caching) COPY package*.json ./ +# Copy workspace package.json files (for workspaces like gui) +COPY src/gui/package*.json ./src/gui/ # Install mocha globally RUN npm install -g mocha @@ -41,11 +43,14 @@ RUN npm cache clean --force && \ fi; \ done +# Ensure html-entities is installed (addressing specific dependency issue) +RUN npm install html-entities + # Copy source files after dependency installation COPY . . -# Build the GUI -RUN cd src/gui && npm run build +# Install GUI dependencies if needed and build +RUN cd src/gui && npm ci && npm run build # Production stage FROM node:23.9-alpine