'Docker build not working with NextJS on node:alpine with SWC

I have a NextJS App that I want to build into a docker image and run as a container later. I'm using the Dockerfile from https://nextjs.org/docs/deployment#docker-image.

When I run docker build . Everything works fine until Step 10/23:

yarn run v1.22.15
$ next build

info  - Checking validity of types...
info  - Creating an optimized production build...

Error: Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /app/node_modules/@next/swc-linux-x64-gnu/next-swc.linux-x64-gnu.node)

I found out that this is caused by SWC and alpine, but does anyone know how to solve this?

Maybe this can help: https://github.com/vercel/next.js/issues/30713



Solution 1:[1]

For us, some of the team members had the older versions of the npm, and that created the problem in package-lock.json.

The solution to this is to delete the node_modules and package-lock.json from the project and run npm install

Note: If are building a docker image and your dockerfile has COPY package*.json ./ line then the new package-lock.json has to be updated to the repository from where the build will happen

Solution 2:[2]

RUN rm -r node_modules/@next/swc-linux-x64-gnu

adding that und yarn install actually fixes that bug

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nithya
Solution 2 Noah