Docker Node Alpine Image Build Fails on node-gyp

40,804

Solution 1

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.

Solution 2

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"]

Solution 3

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

Solution 4

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 
Share:
40,804

Related videos on Youtube

Jason
Author by

Jason

Software engineer, specialized in Web development. Currently working for Carsforsale.com.

Updated on May 11, 2022

Comments

  • Jason
    Jason almost 2 years

    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"]
    
  • soundly_typed
    soundly_typed about 3 years
    Thank you so much, you helped reduced my image size by 72%.
  • Pablo Lopes
    Pablo Lopes about 3 years
    It works for me too. The secret is in this command: RUN apk add g++ make python. Thanks
  • Marcelo Fonseca
    Marcelo Fonseca almost 3 years
    python package is not found. I had to use py3-pip instead
  • sarimarton
    sarimarton over 2 years
    It's simpler to user builder images instead of optimizing with package deletion
  • Phil-R
    Phil-R over 2 years
    Yep, python package doesn't work anymore. Now you should use RUN apk add g++ make py3-pip
  • Kevin Danikowski
    Kevin Danikowski over 2 years
    The @MGLondon answer which slims the container should be the new accepted answer, or this one should be updated
  • Jason
    Jason over 2 years
    @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.
  • Kevin Danikowski
    Kevin Danikowski over 2 years
    @Jason excellent point, I definitely agree
  • khizer
    khizer over 2 years
    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
    khizer over 2 years
    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 :)
  • Jeremy
    Jeremy almost 2 years
    Why is this specific for node:16.13-alpine3.15
  • MadaShindeInai
    MadaShindeInai almost 2 years
    Because i did not test it for another versions. But you can try!