Skip to content

Commit a8b4185

Browse files
committed
Strip npm CLI from runtime image to eliminate its vulnerable dependency tree
1 parent 7e972da commit a8b4185

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

‎Dockerfile‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
# syntax=docker/dockerfile:1
2-
3-
# Stage 1: install dependencies only
4-
FROM node:22-alpine AS deps
5-
WORKDIR /app
6-
COPY package.json package-lock.json ./
7-
RUN npm ci --omit=dev
8-
9-
# Stage 2: slim runtime image
101
FROM node:22-alpine AS runtime
112
WORKDIR /app
123
ENV NODE_ENV=production
134

145
COPY --from=deps /app/node_modules ./node_modules
156
COPY --chown=node:node . .
7+
8+
# The base image bundles the full npm CLI and its dependency tree for
9+
# `npm install`-type use. This container only ever runs `node src/index.js`
10+
# — npm/npx are never invoked at runtime — so remove them rather than ship
11+
# vulnerable code that's never executed.
12+
RUN rm -rf /usr/local/lib/node_modules/npm /usr/local/bin/npm /usr/local/bin/npx /usr/local/bin/corepack
13+
1614
USER node
1715

1816
EXPOSE 3000

0 commit comments

Comments
 (0)