node-gyp support in alpine linux

Grigor Khachatryan - Jul 16 '19 - - Dev Community

Originally published on Medium

When using alpine, you need to install build dependencies for some node module to be able to be built natively.
Here is an example of how you would install dependencies for packages that require node-gyp support on the alpine variant:

FROM node:alpine

RUN apk add --no-cache --virtual .gyp python make g++ \
    && npm install [ your npm dependencies here ] \
    && apk del .gyp
Enter fullscreen mode Exit fullscreen mode

And Here’s a multistage build example:

FROM node:alpine as builder

## Install build toolchain, install node deps and compile native add-ons
RUN apk add --no-cache python make g++
RUN npm install [ your npm dependencies here ]

FROM node:alpine as app

## Copy built node modules and binaries without including the toolchain
COPY --from=builder node_modules .
Enter fullscreen mode Exit fullscreen mode

Pro Tip:

As Alpine Linux uses musl, you may run into some issues with environments expecting glibc-like behavior — especially if you try to use binaries compiled with glibc. You should recompile these binaries to use musl (compiling on Alpine is probably the easiest way to do this).
If you get an error similar to error loading shared library ld-linux-x86-64.so.2, it may be that you have dependencies relying on libc – you can try to fix this by adding:

RUN apk add --no-cache libc6-compat
Enter fullscreen mode Exit fullscreen mode

or

RUN ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2
Enter fullscreen mode Exit fullscreen mode

to your Dockerfile.

Like to learn?

Follow me on twitter where I post all about the latest and greatest JavaScript, AI, DevOps, VR/AR, Technology, and Science! Connect with me on LinkedIn too!

. . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player