Strapi V5. Docker build not working
nazisagit
HOBBYOP

a month ago

--Encountered error:

Error: Failed to load native binding at Object.<anonymous> (/app/node_modules/@swc/core/binding.js:333:11)

--Dockerfile build step EDITED

FROM node:20.19.5-alpine3.21 AS build

WORKDIR /app

COPY package*.json ./

ENV npm_config_ignore_scripts=false

ENV npm_config_optional=true

RUN npm ci

COPY . .

RUN npm run build

--package.json

"dependencies": {

"@strapi/plugin-cloud": "5.29.0",

"@strapi/plugin-users-permissions": "5.29.0",

"@strapi/strapi": "5.29.0",

"fs-extra": "^10.0.0",

"mime-types": "^2.1.27",

"pg": "8.8.0",

"react": "^18.0.0",

"react-dom": "^18.0.0",

"react-router-dom": "^6.0.0",

"strapi-phone-validator-5": "^1.0.0",

"strapi-plugin-multi-select": "^2.1.1",

"styled-components": "^6.0.0",

"@swc/core": "^1.7.26"

}

I am using a node image which should allow for the native binding, and even setting @swc/core as a dependency which should load the compatible library. I am not sure at this point what more I could do.

Solved$10 Bounty

6 Replies

Railway
BOT

a month ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


Railway

Hey there! We've found the following might help you get unblocked faster: - [🧵 Strapi build fail: Failed to load native binding](https://station.railway.com/questions/strapi-build-fail-failed-to-load-native-9085c4ec) - [🧵 Unmable to build basic Node project](https://station.railway.com/questions/unmable-to-build-basic-node-project-f5d1349f) - [🧵 Guidance for Replit → GitHub → Railway deployments (best practices & handoff checklist)](https://station.railway.com/community/guidance-for-replit-git-hub-railway-d-743a3b78) If you find the answer from one of these, please let us know by solving the thread!

nazisagit
HOBBYOP

a month ago

None of those help


sumon9836devil
FREE

a month ago

The issue is that Alpine Linux doesn't have the necessary native bindings for @swc/core. The swc bindings are platform specific, and alpine uses musl libc instead of glibc, which can cause native

module issues.

Here are two solutions

Switch to Debian-based Node image

Use the Dockerfile

FROM node:20.19.5 AS build

WORKDIR /app

COPY package*.json ./

RUN npm ci

COPY . .

RUN npm run build


sumon9836devil

The issue is that Alpine Linux doesn't have the necessary native bindings for @swc/core. The swc bindings are platform specific, and alpine uses musl libc instead of glibc, which can cause nativemodule issues.Here are two solutionsSwitch to Debian-based Node imageUse the DockerfileFROM node:20.19.5 AS buildWORKDIR /appCOPY package*.json ./RUN npm ciCOPY . .RUN npm run build

nazisagit
HOBBYOP

a month ago

FROM node:20.19.5 AS build
WORKDIR /app

# Install dependencies
COPY package*.json ./
RUN npm ci

# Build
COPY . .
RUN npm run build

# Remove dev dependencies after build
RUN npm prune --omit=dev


FROM node:20.19.5 AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=1337

# Copy the built app with pruned dependencies
COPY --from=build /app /app

EXPOSE 1337
CMD ["npm", "run", "start"]

Still running into the same issue at RUN npm run build when strapi is attempting to build the admin panel


dgq-wz

When building a Strapi application within a Docker container using a Node.js Alpine image, you may encounter a "Failed to load native binding" error related to the @swc/core package. This issue typically arises from incompatibilities between the host machine's operating system and the Docker container's environment, particularly with platform-specific native modules.The @swc/core package, a speedy web compiler used by Strapi, relies on native bindings that are specific to the underlying system architecture and C standard library. When you run npm ci within your Docker build, it uses the package-lock.json file to install dependencies. If this lock file was generated on a different platform (like macOS or Windows) than the Docker container (Linux with musl libc in Alpine), the incorrect native binding for @swc/core may be expected, leading to the error.Several users have reported similar issues, highlighting the challenges of platform-specific dependencies in containerized environments.To resolve this, you can try the following solutions:1. Explicitly Install the Correct Native Binding for Alpine LinuxThe official SWC documentation specifies that for Alpine Linux, an additional package, @swc/core-linux-musl, is required.[1][2]You can add this as a dependency in your package.json:codeJSON"dependencies": { ... "@swc/core-linux-musl": "^1.7.26", ... }Then, rebuild your Docker image.2. Modify Your Dockerfile to Address Platform-Specific DependenciesYou can adjust your Dockerfile to ensure the correct dependencies are installed for the Alpine environment. One common approach is to remove the package-lock.json before running npm ci to force it to resolve dependencies based on the container's environment. However, a more robust solution that still leverages the lock file is to regenerate it inside the container.Here's a modified Dockerfile snippet that can help:codeDockerfileFROM node:20.19.5-alpine3.21 AS build WORKDIR /app COPY package.json ./ # This is necessary because the package-lock.json is not copied due to the swc issue (depends on OS) RUN npm install --package-lock-only RUN npm ci COPY . . ENV NODE_ENV production RUN npm run buildAdditionally, consider adding node_modules and package-lock.json to your .dockerignore file to prevent the local versions from being copied into the container.[3]3. Switch to a Debian-Based Node.js ImageIf the issue persists, the problem might be related to the musl C library used by Alpine Linux. Some native modules have better compatibility with the glibc library found in Debian-based images.You can switch your base image to a non-Alpine variant:codeDockerfileFROM node:20.19.5-slimWhile this will result in a slightly larger image, it often resolves issues with native dependencies.[4]4. Clean Installation and Consistent EnvironmentsIn some cases, simply clearing out your local node_modules and package-lock.json and then rebuilding can resolve the issue, as this ensures the lock file is generated from a clean state.[5] It's also important to ensure that all developers on a team are using the same version of npm to avoid inconsistencies in the package-lock.json file.[4]By understanding the root cause of the native binding failure and applying these targeted solutions, you should be able to successfully build your Strapi application in a Docker environment.

nazisagit
HOBBYOP

a month ago

Solution no 2 did make the issue with running the npm run build work, but now I have an issue with Sharp

Could not load js config file /app/node_modules/@strapi/upload/dist/server/index.js: Could not load the "sharp" module using the linux-x64 runtime

nazisagit
HOBBYOP

a month ago

Will open another thread for the new sharp issue


Status changed to Solved brody 29 days ago


Loading...