对应用程序进行 Dockerizing 处理,在运行 npm install 时抛出"npm WARN tar ENOENT:没有这样的文件或目录"



我为我的应用程序创建了一个docker文件。这是我的Dockerfile

FROM node:14.20.1-alpine3.15
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

当尝试使用docker build -t app .它在npm安装指令中失败,出现以下错误

#9 30.84 npm WARN tar ENOENT: no such file or directory, open '/app/node_modules/.staging/@amcharts/amcharts4-geodata-e29272c3/estoniaLow.d.ts'
#9 30.86 npm WARN tar ENOENT: no such file or directory, open '/app/node_modules/.staging/tslint-78b3aa2e/lib/rules/completed-docs/exclusion.d.ts'
#9 30.86 npm WARN tar ENOENT: no such file or directory, open '/app/node_modules/.staging/rxjs-c65985b6/_esm2015/internal/OuterSubscriber.js'
#9 30.87 npm WARN tar ENOENT: no such file or directory, open '/app/node_modules/.staging/rxjs-31d24ad4/operators/onErrorResumeNext.js'
#9 30.87 npm WARN tar ENOENT: no such file or directory, open '/app/node_modules/.staging/@angular/compiler-cli-923906dd/ngcc/src/ngcc_options.js'

我甚至试图清除缓存,并确保用户有足够的权限。

我也遇到了同样的问题。为了调试,我尝试只复制package.json文件,而不是package*.json,重建图像,然后弹出以下错误。

#9 78.90 npm ERR! code ENOENT
#9 78.90 npm ERR! syscall spawn git
#9 78.90 npm ERR! path git
#9 78.91 npm ERR! errno -2
#9 78.93 npm ERR! enoent Error while executing:
#9 78.93 npm ERR! enoent undefined ls-remote -h -t ssh://git@github.com/zingchart/zingchart-constants.git
#9 78.93 npm ERR! enoent
#9 78.94 npm ERR! enoent
#9 78.94 npm ERR! enoent spawn git ENOENT
#9 78.94 npm ERR! enoent This is related to npm not being able to find a file.
#9 78.94 npm ERR! enoent 

作为git,bash不是高山的一部分。所以安装git&bash可以解决这个问题。因此,在npm安装指令之前添加以下命令

RUN apk update && apk upgrade && apk add --no-cache bash git openssh

或者简单地使用apk add --no-cache bash git openssh

如果您正在使用某个用户进行安装,请具有适当的权限(如root(,否则它将导致无法锁定包数据库

Dockerfile看起来像这个

FROM node:14.20.1-alpine3.15
RUN apk add --no-cache bash git openssh
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

最新更新