'Error installing Sharp in docker container

When running an npm install locally everything is fine but as soon as I try it inside my docker container I get the following error:

/api/node_modules/sharp/lib/constructor.js:1
    Something went wrong installing the "sharp" module
    
    Error relocating /api/node_modules/sharp/build/Release/../../vendor/8.10.6/lib/libvips-cpp.so.42: _ZNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEC1Ev: symbol not found

Any help greatly appreciated!

Docker image is incredibly simple:

FROM node:12.13.0-alpine AS alpine

WORKDIR /api

COPY package.json .

RUN npm install


Solution 1:[1]

In my case after trying a lot of different options from scouring the github issues for Sharp, the addition of this line to my Dockerfile fixed it:

RUN npm config set unsafe-perm true

Solution 2:[2]

In case you are using node:15 or later, --unsafe-perm was removed, this is a workaround:

...

RUN chown root.root .  # make sure root own the directory before installing Sharp
RUN npm install

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 Jamie
Solution 2 Tai Vu