简体   繁体   English

Docker 节点高山映像构建在 node-gyp 上失败

[英]Docker Node Alpine Image Build Fails on node-gyp

I'm attempting to Dockerize a Vue.js application.我正在尝试 Dockerize 一个 Vue.js 应用程序。 I'm using the node:10.15-alpine Docker image as a base.我使用node:10.15-alpine Docker 图像作为基础。 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.该应用程序在我的 Ubuntu 机器上运行。 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.错误 output 非常冗长,并且引用了 C/C++ 代码。

Here's my current Dockerfile:这是我目前的 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?谁能帮我用 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.我希望能够使用 Docker 容器运行应用程序,但我需要先构建映像。

Update更新

Since the build was working on my Ubuntu machine, I checked the node version.由于构建在我的 Ubuntu 机器上运行,我检查了node版本。 It was 8.12, so I switch to using the node:8.12-alpine image and the application now works with the following Dockerfile:它是 8.12,所以我切换到使用node:8.12-alpine图像,应用程序现在可以使用以下 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"]

Also stated in my post update, here's the Dockerfile I used to get things working:在我的帖子更新中也有说明,这是我用来让事情正常工作的Dockerfile

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 .如果您的要求要求您的图像最小化空间,请考虑使用RUN apk add --no-cache --virtual [package-list] (而不是apk add [package-list] )安装必要的包,然后使用清除图像中的缓存RUN apk del .gyp

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.由于您在 docker 上使用的是 Alpine 版本,因此您可能希望尽可能少地使用空间,同时修复node-gyp rebuild错误。 For that it is recommended to use the below command ie 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;此外,您应该将apk addnpm install命令组合在一起; this would help in further reducing space between docker cache layers.这将有助于进一步减少 docker 缓存层之间的空间。

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

对于使用node:14-alpine的任何人,这为我修复了它: RUN apk add --no-cache python3 py3-pip make g++

For anyone using node:16.13-alpine3.15 (or close versions):对于使用node:16.13-alpine3.15 (或关闭版本)的任何人:

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 

I started having this problem when I went to Alpine 3.15 (I MAY have skipped 3.14; I can't remember).当我去 Alpine 3.15 时,我开始遇到这个问题(我可能跳过了 3.14;我不记得了)。 Instead of using a node base image, I'm using Microsoft.我使用的是 Microsoft,而不是使用节点基础映像。

It looks like in Alpine 3.13, the npm package was more aligned to the nodejs package.看起来在 Alpine 3.13 中, npm包更符合nodejs包。 In Alpine 3.14, they changed npm to be the actual npm version.在 Alpine 3.14 中,他们将npm更改为实际的npm版本。 So, I added both nodejs and npm instead of just npm (line 2).所以,我添加了nodejsnpm不仅仅是npm (第 2 行)。

Here's my updated Dockerfile:这是我更新的 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.注意: npm i作为 .csproj 中构建的一部分出现。

I resolve this problem by add RUN apk add --no-cache python2 g++ make before npm install我通过在npm 安装之前添加RUN apk add --no-cache python2 g++ make解决了这个问题

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM