Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(build): fix cve vulnerabilities in web and worker docker images (#12732)

Thorsten Spiekerd26b24753503
diff --git a/.dockerignore b/.dockerignore
index 1de652634f..d9a86cca91 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -3,6 +3,13 @@ Dockerfile
node_modules
npm-debug.log
README.md
+.pnpm-store
+**/.pnpm-store
+.turbo
+**/.turbo
**/.next
+**/.next-check
+**/dist
+**/*.tsbuildinfo
.git
-**/node_modules
\ No newline at end of file
+**/node_modules
diff --git a/web/Dockerfile b/web/Dockerfile
index 7663a38e63..51940ed4c5 100644
--- a/web/Dockerfile
+++ b/web/Dockerfile
@@ -3,9 +3,9 @@ FROM --platform=${TARGETPLATFORM:-linux/amd64} node:24-alpine AS alpine
# It's important to update the index before installing packages to ensure you're getting the latest versions.
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
-RUN apk update && apk upgrade --no-cache libcrypto3 libssl3 libc6-compat busybox ssl_client
+RUN apk update && apk upgrade --no-cache libcrypto3 libssl3 libc6-compat busybox ssl_client zlib
-FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine AS base
+FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine AS build-base
# Pin turbo to avoid nondeterministic prune output from future patch releases.
RUN npm install [email protected] --global
ENV PNPM_HOME="/pnpm"
@@ -13,14 +13,19 @@ ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
RUN corepack prepare [email protected] --activate
-FROM --platform=${TARGETPLATFORM:-linux/amd64} base AS pruner
+FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine AS runtime-base
+# Remove package managers from the runtime image; only build stages need them.
+RUN rm -rf /usr/local/lib/node_modules/corepack && \
+ rm -f /usr/local/bin/corepack /usr/local/bin/pnpm /usr/local/bin/pnpx /usr/local/bin/yarn /usr/local/bin/yarnpkg
+
+FROM --platform=${TARGETPLATFORM:-linux/amd64} build-base AS pruner
WORKDIR /app
COPY . .
RUN turbo prune --scope=web --docker
-FROM --platform=${TARGETPLATFORM:-linux/amd64} base AS builder
+FROM --platform=${TARGETPLATFORM:-linux/amd64} build-base AS builder
WORKDIR /app
@@ -88,7 +93,7 @@ ENV CI true
RUN NODE_OPTIONS='--max-old-space-size-percentage=75' turbo run build --filter=web...
# Production image, copy all the files and run next
-FROM --platform=${TARGETPLATFORM:-linux/amd64} base AS runner
+FROM --platform=${TARGETPLATFORM:-linux/amd64} runtime-base AS runner
ARG TARGETPLATFORM
ARG BUILDPLATFORM
@@ -124,6 +129,10 @@ RUN if [ -n "$NEXT_PUBLIC_LANGFUSE_CLOUD_REGION" ]; then \
npm install --no-package-lock --no-save [email protected]; \
fi
+# Runtime images do not need npm once explicit runtime tools are installed.
+RUN rm -rf /usr/local/lib/node_modules/npm && \
+ rm -f /usr/local/bin/npm /usr/local/bin/npx
+
RUN MIGRATE_TARGET_ARCH=$(echo ${TARGETPLATFORM:-linux/amd64} | sed 's/\//-/g') && \
wget -q -O- https://github.com/golang-migrate/migrate/releases/download/v4.19.1/migrate.$MIGRATE_TARGET_ARCH.tar.gz | tar xvz && \
mv migrate /usr/bin/migrate
diff --git a/worker/Dockerfile b/worker/Dockerfile
index 77f6658641..1d1218dac0 100644
--- a/worker/Dockerfile
+++ b/worker/Dockerfile
@@ -3,9 +3,9 @@ FROM --platform=${TARGETPLATFORM:-linux/amd64} node:24-alpine AS alpine
# It's important to update the index before installing packages to ensure you're getting the latest versions.
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
-RUN apk update && apk upgrade --no-cache libcrypto3 libssl3 libc6-compat busybox ssl_client
+RUN apk update && apk upgrade --no-cache libcrypto3 libssl3 libc6-compat busybox ssl_client zlib
-FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine AS base
+FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine AS build-base
# Pin turbo to avoid nondeterministic prune output from future patch releases.
RUN npm install [email protected] --global
ENV PNPM_HOME="/pnpm"
@@ -13,17 +13,19 @@ ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
RUN corepack prepare [email protected] --activate
+FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine AS runtime-base
+# Remove package managers from the runtime image; only build stages need them.
+RUN rm -rf /usr/local/lib/node_modules/corepack /usr/local/lib/node_modules/npm && \
+ rm -f /usr/local/bin/corepack /usr/local/bin/npm /usr/local/bin/npx /usr/local/bin/pnpm /usr/local/bin/pnpx /usr/local/bin/yarn /usr/local/bin/yarnpkg
-FROM --platform=${TARGETPLATFORM:-linux/amd64} base AS pruner
+FROM --platform=${TARGETPLATFORM:-linux/amd64} build-base AS pruner
WORKDIR /app
COPY . .
RUN turbo prune --scope=worker --docker
-
-
-FROM --platform=${TARGETPLATFORM:-linux/amd64} base AS builder
+FROM --platform=${TARGETPLATFORM:-linux/amd64} build-base AS builder
WORKDIR /app
@@ -46,7 +48,24 @@ COPY --from=pruner /app/out/full/ .
RUN turbo run build --filter=worker...
-FROM --platform=${TARGETPLATFORM:-linux/amd64} base AS runner
+FROM --platform=${TARGETPLATFORM:-linux/amd64} builder AS prod-deps
+
+RUN rm -rf /prod && \
+ pnpm --filter worker deploy --prod /prod/worker && \
+ builder_prisma_client_dir="$(find /app/node_modules/.pnpm -path '*/node_modules/@prisma/client' -type d | head -n 1)" && \
+ deployed_prisma_client_dir="$(find /prod/worker/node_modules/.pnpm -path '*/node_modules/@prisma/client' -type d | head -n 1)" && \
+ builder_prisma_runtime_dir="$(dirname "$(dirname "$builder_prisma_client_dir")")/.prisma" && \
+ deployed_prisma_runtime_dir="$(dirname "$(dirname "$deployed_prisma_client_dir")")/.prisma" && \
+ rm -rf "$deployed_prisma_client_dir" "$deployed_prisma_runtime_dir" && \
+ mkdir -p "$(dirname "$deployed_prisma_client_dir")" && \
+ cp -R "$builder_prisma_client_dir" "$deployed_prisma_client_dir" && \
+ cp -R "$builder_prisma_runtime_dir" "$deployed_prisma_runtime_dir" && \
+ # @langfuse/shared currently brings auth-only Next.js packages that the worker never executes.
+ find /prod/worker/node_modules \
+ \( -path '*/node_modules/next' -o -path '*/node_modules/next-auth' -o -path '*/.bin/next' \) \
+ -exec rm -rf {} +
+
+FROM --platform=${TARGETPLATFORM:-linux/amd64} runtime-base AS runner
ARG TARGETPLATFORM
ARG BUILDPLATFORM
@@ -66,10 +85,10 @@ ARG UID=1001
ARG GID=1001
RUN addgroup --system --gid ${GID} expressjs
RUN adduser --system --uid ${UID} expressjs
-USER expressjs
-COPY --from=builder --chown=expressjs:expressjs /app .
-COPY --chown=expressjs:expressjs ./worker/entrypoint.sh ./worker/entrypoint.sh
+
+COPY --from=prod-deps --chown=expressjs:expressjs /prod/worker ./worker
RUN chmod +x ./worker/entrypoint.sh
+USER expressjs
EXPOSE 3030
ENV PORT=3030