我正在尝试在系统上构建映像,但是npm install
在容器中失败,而我可以在浏览器中以及通过终端(ping和curl)访问registry.npmjs.org
。我什至可以在主机上使用npm install
并安装软件包,但我不知道为什么它在容器中失败。
构建日志:
$ ./build.sh
Sending build context to Docker daemon 3.222MB
Step 1/10 : FROM node:12
---> 9dd56f7e705f
Step 2/10 : RUN npm -g install typescript@latest
---> Running in a407e9d81b86
/usr/local/bin/tsc -> /usr/local/lib/node_modules/typescript/bin/tsc
/usr/local/bin/tsserver -> /usr/local/lib/node_modules/typescript/bin/tsserver
+ typescript@3.8.3
added 1 package from 1 contributor in 6.922s
Removing intermediate container a407e9d81b86
---> f02a1b9ee66e
Step 3/10 : COPY package.json /bridge/server/package.json
---> aae5cde286af
Step 4/10 : COPY package-lock.json /bridge/server/package-lock.json
---> 286c8fe066bb
Step 5/10 : WORKDIR /bridge/server
---> Running in 1682562e1a68
Removing intermediate container 1682562e1a68
---> bafd74ae6af0
Step 6/10 : RUN npm install
---> Running in bf96e6d02b67
npm ERR! network timeout at: https://registry.npmjs.org/arr-flatten/-/arr-flatten-1.1.0.tgz
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-05-06T08_53_00_055Z-debug.log
The command '/bin/sh -c npm install' returned a non-zero code: 1
在主机上安装软件包:
$ npm install chance
npm WARN saveError ENOENT: no such file or directory, open '/home/workspace/test/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/home/workspace/test/package.json'
npm WARN wws-k8s No description
npm WARN wws-k8s No repository field.
npm WARN wws-k8s No README data
npm WARN wws-k8s No license field.
+ chance@1.1.4
added 1 package from 1 contributor and audited 1 package in 0.845s
found 0 vulnerabilities
npm在主机上显示的package-lock.json可以访问https://registry.npmjs.org
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"chance": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/chance/-/chance-1.1.4.tgz",
"integrity": "sha512-pXPDSu3knKlb6H7ahQfpq//J9mSOxYK8SMtp8MV/nRJh8aLRDIl0ipLH8At8+nVogVwtvPZzyIzY/EbcY/cLuQ=="
}
}
}
我什至走进了容器并尝试ping和卷曲,我确信即使在容器中,我也可以使用https://registry.npmjs.org:
# inside container
root@9712e650c2c5:~/.npm/_cacache# ping google.com
PING google.com (172.217.20.110) 56(84) bytes of data.
64 bytes from fra02s28-in-f14.1e100.net (172.217.20.110): icmp_seq=1 ttl=53 time=15.8 ms
64 bytes from fra02s28-in-f14.1e100.net (172.217.20.110): icmp_seq=2 ttl=53 time=15.6 ms
64 bytes from fra02s28-in-f14.1e100.net (172.217.20.110): icmp_seq=3 ttl=53 time=15.9 ms
64 bytes from fra02s28-in-f14.1e100.net (172.217.20.110): icmp_seq=4 ttl=53 time=16.1 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 7038ms
rtt min/avg/max/mdev = 15.670/15.877/16.136/0.212 ms
root@9712e650c2c5:~/.npm/_cacache# ping registry.npmjs.org
PING registry.npmjs.org (104.16.21.35) 56(84) bytes of data.
64 bytes from 104.16.21.35 (104.16.21.35): icmp_seq=1 ttl=57 time=12.9 ms
64 bytes from 104.16.21.35 (104.16.21.35): icmp_seq=2 ttl=57 time=12.7 ms
64 bytes from 104.16.21.35 (104.16.21.35): icmp_seq=3 ttl=57 time=12.4 ms
64 bytes from 104.16.21.35 (104.16.21.35): icmp_seq=4 ttl=57 time=12.6 ms
64 bytes from 104.16.21.35 (104.16.21.35): icmp_seq=5 ttl=57 time=12.7 ms
我什至可以在其他目录的容器中安装软件包:
root@9712e650c2c5:~/test# npm install chance
npm WARN saveError ENOENT: no such file or directory, open '/root/test/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/root/test/package.json'
npm WARN test No description
npm WARN test No repository field.
npm WARN test No README data
npm WARN test No license field.
+ chance@1.1.4
added 1 package from 1 contributor and audited 1 package in 5.695s
found 0 vulnerabilities
为什么无法连接到registry.npmjs.org并安装软件包?
主机为Ubuntu 18.04,您可以在下面看到docker和docker-compose版本:
$ docker version
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea838
Built: Wed Nov 13 07:29:52 2019
OS/Arch: linux/amd64
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea838
Built: Wed Nov 13 07:28:22 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683
$ docker-compose version
docker-compose version 1.24.0, build 0aa59064
docker-py version: 3.7.2
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.1.0j 20 Nov 2018