linux-x64 binaries cannot be used on the linuxmusl-x64 platform error

It's caused by the fact you run docker on a Linux platform and your machine is probably mac or windows. Most of the time you can use the same module versions but not when it uses low level kernel functions like sharp.

You need a different version of Sharp on Docker and on your local machine.

You have probably run your project without docker, then with docker.

Solution 1: You can remove package.lock + node_modules folder then rebuild and now only use docker.

Solution 2: (not clean but can help) Remove Sharp from you package.json and install it later when you start your server. For example by updating your package.json:

package.json

{
  ...
  "scripts": {
    ...
    "start-docker": "yarn add sharp && nodemon index.js"
  },
  ...

you can do it in your Dockerfile file as well:

Dockerfile

FROM node:13
ADD package.json /package.json
RUN yarn install
RUN yarn add sharp
ENV NODE_PATH=/node_modules
ENV PATH=$PATH:/node_modules/.bin
WORKDIR /app
CMD ["yarn","start-docker"]

I faced the problem with multi-staged docker file where the two imagse are based on different platforms and I solved it like this:

FROM node:14 AS builder
WORKDIR /app
COPY ./package.json ./
RUN npm install
COPY . .
RUN npm run build

FROM node:14-alpine
WORKDIR /app
COPY --from=builder /app ./
RUN npm install sharp
CMD ["npm", "run", "start:prod"]

The trick is to install run npm install sharp in the final container - in my case it was Alpine linux that is different than the base image of node:14 (obviously it is different platform). Sharp is compiled directly to certain platform so running npm install in one container and copy those compiled / precompiled files to another container cannot work. I assume this is still better solution then fallback to node:14 image (only run container) that is way bigger (in my case 1,4Gb -> 0.7Gb Alpine).

Keep in mind that you should still have .dockeringore file with node_modules won't help you to solve this issue. It just speeds up process building with cache on CI server or on localhost (with different OS).

Cheers


I faced the same error with Docker. The problem was that I forgot to include a .dockerignore file and my node_modules were being copied into the container.

Try creating a .dockerignore file in the root of your project (next to your Dockerfile) with e.g.:

node_modules
npm-debug.log
Dockerfile
.dockerignore
.git
.gitignore