Docker Node Alpine镜像构建在node-gyp上失败

nwnhqdif  于 2023-10-16  发布在  Docker
关注(0)|答案(8)|浏览(168)

我正在尝试Dockerize一个Vue.js应用程序。我使用node:10.15-alpine Docker镜像作为基础。映像构建失败,并出现以下错误:

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.

应用程序运行在我的Ubuntu机器上。我在网上搜索了一个解决方案。
我试过:

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

这也失败了。错误输出相当冗长,并且引用了C/C++代码。
以下是我当前的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"]

谁能帮我解决这个问题与node-gyp?我希望能够使用Docker容器运行应用程序,但我需要先获得要构建的映像。

更新

由于构建在我的Ubuntu机器上工作,我检查了node版本。它是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"]
mm9b1k5b

mm9b1k5b1#

在我的帖子更新中也提到,这是我用来让事情工作的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"]

如果您的需求要求映像最大限度地减少空间,请考虑使用RUN apk add --no-cache --virtual [package-list](而不是apk add [package-list])安装必要的软件包,然后使用RUN apk del .gyp清除映像中的该高速缓存。

p1tboqfb

p1tboqfb2#

由于您在docker上使用的是Alpine版本,因此在修复node-gyp rebuild错误时,您可能希望使用尽可能少的空间。为此,建议使用下面的命令,即。而不使用高速缓存并且具有可以稍后删除的虚拟包。您还应该将apk addnpm install命令合并组合在一起;这将有助于进一步减少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"]
am46iovg

am46iovg3#

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

qni6mghb

qni6mghb4#

对于任何使用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
qyswt5oh

qyswt5oh5#

我通过在npm install之前添加RUN apk add --no-cache python 2 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"]
waxmsbnn

waxmsbnn6#

我开始有这个问题时,我去阿尔卑斯山3.15(我可能跳过3.14;我不记得了)。我使用的不是节点基础映像,而是Microsoft。
看起来在Alpine 3.13中,npm包与nodejs包更加一致。在Alpine 3.14中,他们将npm更改为实际的npm版本。因此,我添加了nodejsnpm,而不是justnpm(第2行)。
以下是我更新的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"]

注意:npm i作为. csproj中构建的一部分出现。

iezvtpos

iezvtpos7#

由于某种原因,我开始看到这个问题时,从节点v18.18.0升级到v18.18.1。通过在我的RUN yarn install步骤之前添加yarn global add node-gyp来修复它:

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

20jt8wwn8#

我在这里尝试使用Dockerfile,它工作正常

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

相关问题