javascriptlinuxdockernestjssharp

Could not load the "sharp" module using the linux-x64 runtime


I am working on a nest js project and I am using sharp library, suddenly I am starting to get error when trying to run the docker container here is my dockerfile

FROM node:20-alpine AS builder
WORKDIR /app
COPY . .
RUN yarn
RUN npx nx build api --production=true

FROM node:20-alpine AS runner
WORKDIR /app

COPY --from=builder /app/package.json ./
COPY --from=builder /app/yarn.lock ./

RUN yarn install --production
RUN yarn add sharp --ignore-engines

COPY --from=builder /app/dist/apps/api ./
ARG PORT=3333
ENV PORT=${PORT}

EXPOSE ${PORT}

CMD [ "node", "main" ]

I tried upgrading and downgrading sharp version, and still didn't work


Solution

  • Instead of Alpine, use the standard node.js image that is built on the boxes. This will give Sharp the same environment it is used to Change the Dockerflare from lines like this

    After this change, reload the Docker image, for example, the issue will be resolved.

    # BUILDER STAGE
    FROM node:20 AS builder
    WORKDIR /app
    COPY . .
    RUN yarn
    RUN npx nx build api --production=true
    
    # RUNNER STAGE
    FROM node:20 AS runner
    WORKDIR /app
    
    COPY --from=builder /app/package.json ./
    COPY --from=builder /app/yarn.lock ./
    
    RUN yarn install --production
    
    COPY --from=builder /app/dist/apps/api ./
    ARG PORT=3333
    ENV PORT=${PORT}
    
    EXPOSE ${PORT}
    
    CMD [ "node", "main" ]