69

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: fsevents@1.2.4 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! inotify@1.4.2 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the inotify@1.4.2 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"]
Jason
  • 2,382
  • 1
  • 16
  • 29
  • 4
    This issue on GitHub might give some insight: [Document how to use alpine with dependencies that rely on node-gyp](https://github.com/nodejs/docker-node/issues/282) – zero298 Jan 29 '19 at 20:35
  • 2
    Thanks @zero298 I did view that issue. I found a solution and updated my post. Cheers! – Jason Jan 29 '19 at 20:51

6 Answers6

71

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.

Jason
  • 2,382
  • 1
  • 16
  • 29
  • 5
    It works for me too. The secret is in this command: `RUN apk add g++ make python`. Thanks – Pablo Lopes Mar 09 '21 at 20:15
  • 8
    python package is not found. I had to use py3-pip instead – Marcelo Fonseca Jun 02 '21 at 17:51
  • 2
    Yep, python package doesn't work anymore. Now you should use `RUN apk add g++ make py3-pip` – Phil-R Sep 05 '21 at 01:16
  • The @MGLondon answer which slims the container should be the new accepted answer, or this one should be updated – Kevin Danikowski Sep 14 '21 at 11:55
  • 1
    @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. – Jason Sep 14 '21 at 17:18
  • @Jason excellent point, I definitely agree – Kevin Danikowski Sep 14 '21 at 22:44
  • This helped me. I had to add `USER root` before `RUN apk add g++ make py3-pip` to make it work. – Alex Jul 12 '23 at 20:01
25

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"]
MGLondon
  • 1,557
  • 15
  • 18
  • 2
    Thank you so much, you helped reduced my image size by 72%. – soundly_typed Feb 17 '21 at 06:15
  • It's simpler to user builder images instead of optimizing with package deletion – sarimarton Aug 02 '21 at 19:32
  • https://stackoverflow.com/a/69477720/6618218 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 ALOT – khizer Oct 07 '21 at 08:39
  • 1
    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 :) – khizer Oct 07 '21 at 08:43
9

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

Micha
  • 523
  • 10
  • 26
8

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 
MadaShindeInai
  • 432
  • 4
  • 16
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"]
yyater97
  • 1,697
  • 2
  • 9
  • 5
  • 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: 127` – user1034912 Mar 15 '23 at 09:46
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.

DaleyKD
  • 417
  • 1
  • 4
  • 18