Docker Node Alpine Image Build Fails on node-gyp
For anyone using node:14-alpine
, this fixed it for me: RUN apk add --no-cache python3 py3-pip make g++
For anyone using node:16.13-alpine3.15
(or close versions):
FROM node:16.13-alpine3.15
RUN apk --no-cache add --virtual .builds-deps build-base python3
WORKDIR /app
COPY package*.json ./
RUN npm install --production && npm rebuild bcrypt --build-from-source && npm cache clean --force
Also stated in my post update, here's the Dockerfile
I used to get things working:
FROM node:8.12-alpine
RUN apk add g++ make py3-pip
EXPOSE 8080
RUN mkdir /app
WORKDIR /app
COPY . /app
RUN npm install
CMD ["npm", "start"]
If your requirements demand your image minimize space, consider installing necessary packages with RUN apk add --no-cache --virtual [package-list]
(instead of apk add [package-list]
) and afterward clearing the cache in the image with RUN apk del .gyp
.
Since you are using an Alpine version on docker, you may want to use as little space as you can, while fixing the node-gyp rebuild
error. For that it is recommended to use the below command i.e. without using a cache and with a virtual package which can be deleted later on. Also you should combine the apk add
and npm install
commands together; this would help in further reducing space between docker cache layers.
FROM node:8.12-alpine
EXPOSE 8080
WORKDIR /app
COPY . .
RUN apk add --no-cache --virtual .gyp \
python \
make \
g++ \
&& npm install \
&& apk del .gyp
CMD ["npm", "start"]