Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Node Alpine Image Build Fails on node-gyp

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"] 
like image 956
Jason Avatar asked Jan 29 '19 19:01

Jason


People also ask

What is node-gyp build?

js native addon build tool. node-gyp is a cross-platform command-line tool written in Node. js for compiling native addon modules for Node. js. It contains a vendored copy of the gyp-next project that was previously used by the Chromium team, extended to support the development of Node.

Do I need to install node-gyp?

node-gyp dependencies If you only need to compile add-ons during the project setup, Node. js should cover it for you. However, if you are an add-on developer, you probably need to install node-gyp globally. To use node-gyp, first, we'll need to install a Python runtime, the make utility, and a C or C++ compiler.

Why is node-gyp required?

node-gyp is a tool that enables the compilation of native add-on modules for Node in multiple platforms. It has widespread use and is included as a dependency in many NPM packages. On most systems, this isn't an issue, and installing node-gyp with the rest of your packages works as expected.


2 Answers

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 python 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 api add [package-list]) and afterward clearing the cache in the image with RUN apk del .gyp.

like image 167
Jason Avatar answered Sep 17 '22 12:09

Jason


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"] 
like image 40
MGLondon Avatar answered Sep 17 '22 12:09

MGLondon