Docker Node Alpine Image Build Fails on node-gyp
Asked Answered
C

8

81

I'm attempting to Dockerize a Vue.js application. I'm using the node:10.15-alpine Docker image as a base. The image build fails with the following error:

gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:484:19)
gyp ERR! stack     at PythonFinder.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:406:16)
gyp ERR! stack     at F (/usr/local/lib/node_modules/npm/node_modules/which/which.js:68:16)
gyp ERR! stack     at E (/usr/local/lib/node_modules/npm/node_modules/which/which.js:80:29)
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/which/which.js:89:16
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/isexe/index.js:42:5
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/isexe/mode.js:8:5
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:154:21)
gyp ERR! System Linux 4.9.125-linuxkit
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /app/node_modules/inotify
gyp ERR! node -v v10.15.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

The application runs on my Ubuntu machine. And I've searched for a resolution online.

I tried:

FROM node:10.15-alpine
EXPOSE 8080
RUN mkdir -p /app/src
WORKDIR /app
COPY build/ config/ dist/ static/ .babelrc .postcssrc.js index.html /app/
COPY package*.json ./
RUN apk add --no-cache make gcc g++ python && \
  npm install --production --silent && \
  apk del make gcc g++ python
ADD src/ /app/src/
CMD ["npm", "start"]

This fails too. The error output is quite verbose and references C/C++ code.

Here's my current Dockerfile:

FROM node:10.15-alpine
EXPOSE 8080
RUN mkdir -p /app/src
WORKDIR /app
COPY build/ config/ dist/ static/ .babelrc .postcssrc.js index.html /app/
COPY package*.json ./
RUN npm install
ADD src/ /app/src/
CMD ["npm", "start"]

Can anyone help me to resolve this issue with node-gyp? I'd like to be able to run the application from with a Docker container, but I need to get the image to build first.

Update

Since the build was working on my Ubuntu machine, I checked the node version. It was 8.12, so I switch to using the node:8.12-alpine image and the application now works with the following Dockerfile:

FROM node:8.12-alpine
RUN apk add g++ make python
EXPOSE 8080
RUN mkdir /app
WORKDIR /app
COPY . /app
RUN npm install
CMD ["npm", "start"]
Claraclarabella answered 29/1, 2019 at 19:45 Comment(2)
This issue on GitHub might give some insight: Document how to use alpine with dependencies that rely on node-gypHeterocercal
Thanks @Heterocercal I did view that issue. I found a solution and updated my post. Cheers!Claraclarabella
C
87

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.

Claraclarabella answered 31/12, 2019 at 0:25 Comment(7)
It works for me too. The secret is in this command: RUN apk add g++ make python. ThanksMickel
python package is not found. I had to use py3-pip insteadBushwhacker
Yep, python package doesn't work anymore. Now you should use RUN apk add g++ make py3-pipDenigrate
The @Allen answer which slims the container should be the new accepted answer, or this one should be updatedTallia
@KevinDanikowski, On the point of order you raise, the original question didn't mention anything about minimizing image size so not having that in the answer should be acceptable - I went ahead and added it on your suggestion though. My answer was here and accepted before @MGLondon's; I think it would have been more appropriate for that info to have been added as an edit to the accepted answer.Claraclarabella
@Claraclarabella excellent point, I definitely agreeTallia
This helped me. I had to add USER root before RUN apk add g++ make py3-pip to make it work.Campbellite
A
27

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"]
Allen answered 28/8, 2020 at 13:19 Comment(4)
Thank you so much, you helped reduced my image size by 72%.Arrhythmia
It's simpler to user builder images instead of optimizing with package deletionWoodford
https://mcmap.net/q/196472/-issue-to-node-sass-and-docker Problem i was facing and just added RUN apk add --no-cache --virtual .gyp \ python \ make \ g++ ` IT WORKED. Was it was issue to compilation to C++ at native level? Can you check error that i was facing on URL above was it due missing g++, python thing`? BTW, THANKS ALOTBrade
I was using FROM node:14.18-alpine just curios will it work on full image like FROM node:14` ? was it due alpine image? Sorry for NOOB questions :)Brade
C
9

For anyone using node:14-alpine, this fixed it for me: RUN apk add --no-cache python3 py3-pip make g++

Claresta answered 1/1, 2022 at 3:48 Comment(0)
P
9

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 
Parquetry answered 21/1, 2022 at 15:17 Comment(3)
Why is this specific for node:16.13-alpine3.15Marking
Because i did not test it for another versions. But you can try!Parquetry
May your enemies tremble at the sound of your name, thanks it worked for meKure
J
1

I resolve this problem by add RUN apk add --no-cache python2 g++ make before npm install

# Stage 1, based on Node.js, to build and compile the react app
FROM node:12-alpine as build

RUN mkdir -p /app
WORKDIR /app
COPY package*.json /app/
COPY ./ /app/

#### => add this script to resolve that problem
RUN apk add --no-cache python2 g++ make

RUN npm install \
    && npm run build        
# Stage 2, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.17-alpine

RUN mkdir -p /app
WORKDIR /app

RUN apk add --update bash jq curl \
    && rm -rf /var/cache/apk/*

COPY --from=build /app/build/ /app/build

COPY ./scripts /app/scripts
COPY .env /app/scripts/.env
COPY ./nginx.conf.template /etc/nginx/conf.d/nginx.conf.template

RUN chmod +x /app/scripts/getEnv.sh \
    && chmod +x /app/scripts/run.sh

CMD ["/bin/bash", "-c", "/app/scripts/run.sh"]
Jocosity answered 10/9, 2022 at 0:59 Comment(1)
This answer is probably out dated now (2023). Error: /bin/sh: 1: apk: not found, Error response from daemon: The command '/bin/sh -c apk add --no-cache python2 g++ make' returned a non-zero code: 127Featherston
A
0

I started having this problem when I went to Alpine 3.15 (I MAY have skipped 3.14; I can't remember). Instead of using a node base image, I'm using Microsoft.

It looks like in Alpine 3.13, the npm package was more aligned to the nodejs package. In Alpine 3.14, they changed npm to be the actual npm version. So, I added both nodejs and npm instead of just npm (line 2).

Here's my updated Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS publish
RUN apk add --no-cache nodejs npm
WORKDIR /src

COPY . .
RUN dotnet restore "MyWeb.csproj" -r linux-musl-x64
RUN dotnet publish "MyWeb.csproj" -c Release -o /app/publish -r linux-musl-x64 --self-contained false --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:5.0-alpine
WORKDIR /app
COPY --from=publish /app/publish .

ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false \
    LC_ALL=en_US.UTF-8 \
    LANG=en_US.UTF-8
RUN apk add --no-cache icu-libs

ENTRYPOINT ["./MyWeb"]

NOTE: The npm i occurs as part of the build in the .csproj.

Alger answered 7/6, 2022 at 15:56 Comment(0)
L
0

For some reason I started seeing this issue when upgrading from node v18.18.0 to v18.18.1. Fixed it by adding a yarn global add node-gyp before my RUN yarn install step:

from node:18.18-alpine

... copy deps, etc ...

RUN yarn global add node-gyp # <-- FIX HERE

RUN NODE_ENV=production yarn install --frozen-lockfile

... copy build app ...
Lase answered 12/10, 2023 at 14:16 Comment(0)
V
0

I try with Dockerfile here, it working

FROM arm64v8/node:18-alpine

RUN apk update
RUN apk add --no-cache make gcc g++ libc6-compat bash python3

RUN yarn global add node-gyp

WORKDIR /usr/src/app

COPY package.json ./
COPY yarn.lock ./

RUN yarn

COPY . .

CMD ["yarn", "dev"]
Visually answered 16/10, 2023 at 10:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.