49

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
  • 1,719
  • 1
  • 12
  • 25
  • 3
    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

4 Answers4

44

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
  • 1,719
  • 1
  • 12
  • 25
  • 4
    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
  • 6
    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
19

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,230
  • 9
  • 17
  • 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 – Márton Sári 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
4

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

Micha
  • 459
  • 8
  • 22
3

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