Jason
Jason

Reputation: 2850

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

Upvotes: 87

Views: 110958

Answers (8)

hacoi1day
hacoi1day

Reputation: 11

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

Upvotes: 1

Jack Ratner
Jack Ratner

Reputation: 111

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 ...

Upvotes: 0

yyater97
yyater97

Reputation: 2983

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

Upvotes: 1

DaleyKD
DaleyKD

Reputation: 427

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.

Upvotes: 0

Jason
Jason

Reputation: 2850

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.

Upvotes: 92

MadaShindeInai
MadaShindeInai

Reputation: 590

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 

Upvotes: 9

Micha
Micha

Reputation: 563

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

Upvotes: 9

MGLondon
MGLondon

Reputation: 1657

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

Upvotes: 27

Related Questions