npm установка не выполняется при создании образа - PullRequest
0 голосов
/ 06 мая 2020

Я пытаюсь создать образ в своей системе, но npm install не работает в контейнере, в то время как я могу получить доступ к registry.npmjs.org как в браузере, так и через терминал (ping и curl). Я даже могу использовать 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

И блокировка пакета. json, который был создан npm на хосте, который показывает, что он может получить доступ к 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 и curl и уверен, что даже в контейнере у меня есть доступ к 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

I я даже могу устанавливать пакеты в контейнере в другие каталоги:

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

Почему не удается подключиться к реестру. 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

...