sharp is a Node.js image processing library that we used to resize avatar images in the backend. We run into problems running sharp on a Docker container using the Node.js Alpine Linux image as it cannot find the binary dependency for Alpine Linux.
According to the docs, we can setup sharp as a cross-platform installation as developers may use different environments. In our development environment, we install sharp this way:
npm install --save sharp npm rebuild --arch=x64 --platform=linux --libc=musl sharp npm rebuild --arch=x64 --platform=linux --libc=glibc sharp
First, we install it using the default behavior that detects the current environment like Windows for example. Then we add more installations for Alpine Linux and Debian based Linux like Ubuntu. This should work for now. This is not enough though as we need to tweak our Dockerfile to rebuild sharp for Alpine Linux.
This is our updated
Dockerfile. It simply installs whatever was installed as specified by the
package-lock.json then rebuild
sharp again to download the correct binary for Alpine Linux.
FROM node:18.16.0-alpine3.17 WORKDIR /app COPY . . RUN chown -R node:node /app USER node RUN npm install && \ npm rebuild --arch=x64 --platform=linux --libc=musl sharp && \ npm run build EXPOSE 3000 CMD ["node", "dist/main.js"]
It works flawlessly.
Featured image by Azis Js.