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
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 .
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
or
RUN ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2
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!
Top comments (8)
When executing "RUN apk add --no-cache python make g++" then I am running into error "unable to select packages: python (no such package): required by: world[python]". I am using Docker image "node:16-alpine".
Use python3 instead.
Have you found the solution?
I found the solution. If you want exactly python2, you need to use the alpine version 3.15. The latest versions only supports python3. So basically change "node:16-alpine" to "node:16-alpine3.15"
I used python2 as I saw it was explicitly looking for python2
If this is your error, I used
python2
instead ofpython
. Thus the command becameapk add --no-cache --virtual .gyp python2 make g++
. Someone suggested to usepython3
. I'm a frontend guy, so you can just try his suggestion.I had to install the npm package
canvas
and in order to be able to do it I had to changeRUN apk add --no-cache --virtual .gyp python make g++
toRUN apk add --no-cache --virtual .gyp python make g++ pkgconfig pixman-dev cairo-dev pango-dev
Hey there! I shared your article here t.me/theprogrammersclub and check out the group if you haven't already!