Docker无法安装npm包

2024-04-09

我在 ubuntu 服务器上运行 docker。

看来是docker的问题。从 ubuntu 安装 package.json 工作正常。

npm http request GET https://registry.npmjs.org/express
npm info retry will retry, error on last attempt: Error: getaddrinfo EAI_AGAIN registry.npmjs.org:443

看来 npm 注册表无法访问。有任何想法吗?

平工作正常!

Dockerfile:

FROM node:4-slim

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/

RUN npm install

COPY . /usr/src/app

EXPOSE 3000

CMD [ "node", "server.js" ]

构建日志:

Sending build context to Docker daemon  66.05kB
Step 1/8 : FROM node:4-slim
 ---> cd692e27dc7f
Step 2/8 : RUN mkdir -p /usr/src/app
 ---> Running in 876d5978bdd7
 ---> 4bab541344aa
Removing intermediate container 876d5978bdd7
Step 3/8 : WORKDIR /usr/src/app
 ---> d66516529b5e
Removing intermediate container 6317ee72e20e
Step 4/8 : COPY package.json /usr/src/app/
 ---> 70483f422380
Removing intermediate container c3781f622ce4
Step 5/8 : RUN npm install
 ---> Running in 37d8afdf1c21
npm info it worked if it ends with ok
npm info using [email protected] /cdn-cgi/l/email-protection
npm info using [email protected] /cdn-cgi/l/email-protection
npm WARN package.json [email protected] /cdn-cgi/l/email-protection No description
npm WARN package.json [email protected] /cdn-cgi/l/email-protection No repository field.
npm WARN package.json [email protected] /cdn-cgi/l/email-protection No README data
npm info preinstall [email protected] /cdn-cgi/l/email-protection
npm info attempt registry request try #1 at 10:58:13 AM
npm http request GET https://registry.npmjs.org/express
npm info attempt registry request try #1 at 10:58:13 AM
npm http request GET https://registry.npmjs.org/mongoose
npm info retry will retry, error on last attempt: Error: getaddrinfo EAI_AGAIN registry.npmjs.org:443
npm info retry will retry, error on last attempt: Error: getaddrinfo EAI_AGAIN registry.npmjs.org:443
npm info attempt registry request try #2 at 10:59:04 AM
npm http request GET https://registry.npmjs.org/mongoose
npm info attempt registry request try #2 at 10:59:04 AM
npm http request GET https://registry.npmjs.org/express
npm info retry will retry, error on last attempt: Error: getaddrinfo EAI_AGAIN registry.npmjs.org:443
npm info retry will retry, error on last attempt: Error: getaddrinfo EAI_AGAIN registry.npmjs.org:443
npm info attempt registry request try #3 at 11:00:44 AM
npm http request GET https://registry.npmjs.org/mongoose
npm info attempt registry request try #3 at 11:00:44 AM
npm http request GET https://registry.npmjs.org/express
npm ERR! Linux 4.4.0-78-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v4.8.3
npm ERR! npm  v2.15.11
npm ERR! code EAI_AGAIN
npm ERR! errno EAI_AGAIN
npm ERR! syscall getaddrinfo

npm ERR! getaddrinfo EAI_AGAIN registry.npmjs.org:443
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:
npm ERR!     /usr/src/app/npm-debug.log

这也是我在 Docker for Windows 和 GitLab Windows runner 上遇到的问题。问题在于docker run网络工作正常并且能够 ping/解析所有域。跑步时docker buildnpm 将失败并出现 EAI_AGAIN 错误。

解决

docker build--network=可以用来Docker 构建文档 https://docs.docker.com/engine/reference/commandline/build/

找到您用于 docker run 或 docker-compose 项目的工作 Docker 网络。

  • 查找docker网络:docker network ls
  • 使用上面的网络运行 Docker 构建docker build --network=<insert network from above> -f "myproj\Dockerfile" -t app:latest
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Docker无法安装npm包 的相关文章

随机推荐