From 7cc73e12543e11d66d30b43cab935736ae2b3f16 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 13 Jun 2025 05:14:42 +0000 Subject: [PATCH] Refactor Dockerfiles to use multi-stage builds This change modifies the Dockerfiles in `genkit` and `socket.io` to implement multi-stage builds. The first stage, named 'builder', is responsible for installing dependencies (including devDependencies like npm) and building the application. The second stage creates a lean production image by copying only the necessary artifacts (the `dist` folder and production `node_modules`) from the 'builder' stage. This approach significantly reduces the final image size and improves security by excluding npm and other development tools from the production environment. --- genkit/Dockerfile | 13 ++++++++++--- socket.io/Dockerfile | 13 ++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/genkit/Dockerfile b/genkit/Dockerfile index 51469df..fb586ff 100644 --- a/genkit/Dockerfile +++ b/genkit/Dockerfile @@ -1,15 +1,22 @@ -FROM node:22-alpine +# Builder stage +FROM node:22-alpine AS builder WORKDIR /app COPY package*.json ./ - RUN npm install COPY . . - RUN npm run build +# Production stage +FROM node:22-alpine + +WORKDIR /app + +COPY --from=builder /app/dist ./dist +COPY --from=builder /app/node_modules ./node_modules + ENV NODE_ENV=production EXPOSE 4001 diff --git a/socket.io/Dockerfile b/socket.io/Dockerfile index 315cd3e..b3e7442 100644 --- a/socket.io/Dockerfile +++ b/socket.io/Dockerfile @@ -1,15 +1,22 @@ -FROM node:22-alpine +# Builder stage +FROM node:22-alpine AS builder WORKDIR /app COPY package*.json ./ - RUN npm install COPY . . - RUN npm run build +# Production stage +FROM node:22-alpine + +WORKDIR /app + +COPY --from=builder /app/dist ./dist +COPY --from=builder /app/node_modules ./node_modules + ENV NODE_ENV=production EXPOSE 3001