Docker构建版本npm安装错误网络超时

7cjasjjr  于 2023-03-22  发布在  Docker
关注(0)|答案(3)|浏览(316)

我正在尝试将镜像构建到node应用程序,但当我执行docker build时显示错误,请运行npm install

npm ERR!网络超时时间:https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz

按照docker文件内容:

FROM node:13

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

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

关注我的package.json

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Bruno Cassiamani",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "config": "^3.3.1",
    "consign": "^0.1.6",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "request": "^2.88.2",
    "xml2js": "^0.4.23"
  }
}

操作系统版本:CentOS Linux release 8.1.1911(Core)
Docker发布:Docker版本19.03.8,构建afacb 8b
打印错误:https://i.imgur.com/GxRgpEG.png

kuhbmx9i

kuhbmx9i1#

[解决方案] Docker网络出现问题,然后我执行以下命令:

docker build -t cassiamani/nodeapp --network=host .

--network=host从host使用dns,对我来说,它的工作。
参考:https://www.linode.com/community/questions/19710/docker-npm-timeout-on-linode

u0njafvf

u0njafvf2#

在我的情况下,工作解决方案是清理docker镜像缓存

docker image prune

并确保你可以在所需的docker镜像中获得所需的npm包。例如,我使用了node:lts-alpine。所以,你必须运行

docker run -it node:lts-alpine sh

并运行(它是一些依赖的“yorkie”包),以确保它可以解析网络地址并下载文件。

wget https://registry.npmjs.org/yorkie/-/yorkie-2.0.0.tgz
hkmswyz6

hkmswyz63#

我知道这来得很晚,但它可能会帮助别人,下面的解决方案对我有效。
1.创建/etc/docker/daemon. json
1.把下面的代码放进去

{
  "dns": ["1.1.1.1", "8.8.8.8"]
}

1.最后运行systemctl daemon-reloadsystemctl restart docker.service

相关问题