@@ -17,7 +17,13 @@ RUN apk add --no-cache curl tar && \
1717 rm -rf /tmp/mem0.tar.gz /tmp/mem0-${MEM0_VERSION} /app/node_modules /app/.next
1818
1919# BUILD ########################################################################
20- FROM base AS builder
20+ # Runs on the build platform (no QEMU emulation, which intermittently crashes
21+ # node/pnpm with "Illegal instruction" on arm64). The Next.js standalone output
22+ # is plain JS, except for the native sharp binaries, so install the optional
23+ # dependencies of every target architecture and let sharp pick one at runtime.
24+ FROM --platform=$BUILDPLATFORM node:20-alpine AS builder
25+ RUN apk add --no-cache libc6-compat
26+ WORKDIR /app
2127
2228COPY --from=source /app /app
2329
@@ -26,7 +32,8 @@ ENV NEXT_TELEMETRY_DISABLED=1
2632ENV NEXT_PUBLIC_API_URL=NEXT_PUBLIC_API_URL
2733ENV NEXT_PUBLIC_INSTANCE_NAME=NEXT_PUBLIC_INSTANCE_NAME
2834
29- RUN corepack enable pnpm && \
35+ RUN printf '\n supportedArchitectures:\n os: [linux]\n libc: [musl]\n cpu: [x64, arm64]\n ' >> pnpm-workspace.yaml && \
36+ corepack enable pnpm && \
3037 pnpm install && \
3138 pnpm build
3239
@@ -45,6 +52,21 @@ RUN addgroup --system --gid 1001 nodejs && \
4552COPY --from=builder /app/public ./public
4653COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
4754COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
55+ # The standalone trace only contains the sharp binaries of the build platform,
56+ # so add the ones matching the target platform from the builder's pnpm store.
57+ ARG TARGETARCH
58+ RUN --mount=from=builder,source=/app/node_modules/.pnpm,target=/mnt/pnpm \
59+ arch=$([ "${TARGETARCH}" = "amd64" ] && echo x64 || echo arm64) && \
60+ cd /app/node_modules/.pnpm && \
61+ for pkg in /mnt/pnpm/@img+sharp-linuxmusl-${arch}@* /mnt/pnpm/@img+sharp-libvips-linuxmusl-${arch}@*; do \
62+ [ -e "$(basename " ${pkg}")" ] || cp -a "${pkg}" . ; \
63+ done && \
64+ for dir in sharp@*/node_modules/@img; do \
65+ for pkg in @img+sharp-linuxmusl-${arch}@*; do \
66+ ln -sfn "../../../${pkg}/node_modules/@img/sharp-linuxmusl-${arch}" "${dir}/sharp-linuxmusl-${arch}" ; \
67+ done; \
68+ done && \
69+ chown -R nextjs:nodejs /app/node_modules
4870COPY --from=builder --chown=nextjs:nodejs /app/entrypoint.sh /home/nextjs/entrypoint.sh
4971RUN chmod 755 /home/nextjs/entrypoint.sh
5072
0 commit comments