npm安装在docker build中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了npm安装在docker build中相关的知识,希望对你有一定的参考价值。
我在本地使用Oracle VM Virtualbox将nodejs
上的ubuntu 17.10
应用程序停靠。
我在Dockerfile中提到了以下步骤:
FROM node:7
WORKDIR /home/ubuntu/Downloads/nodejs/workdirectory
COPY package.json /home/ubuntu/Downloads/nodejs/workdirectory
RUN npm install
COPY . /home/ubuntu/Downloads/nodejs/workdirectory
CMD node index.js
EXPOSE 8081
我正面临以下错误:
ubuntu@ubuntu-VirtualBox:~/Downloads/nodejs/application$ sudo docker build -t acc/nodejsapp2:1.0 .
Sending build context to Docker daemon 4.096kB
Step 1/7 : FROM node:7
---> d9aed20b68a4
Step 2/7 : WORKDIR /home/ubuntu/Downloads/nodejs/workdirectory
---> Using cache
---> 0efd4825ed8f
Step 3/7 : COPY package.json /home/ubuntu/Downloads/nodejs/workdirectory
---> Using cache
---> 5c1ef3d889b5
Step 4/7 : RUN npm install
---> Running in f0bbfa9920cb
npm info it worked if it ends with ok
npm info using npm@4.2.0
npm info using node@v7.10.1
npm info attempt registry request try #1 at 8:46: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 attempt registry request try #2 at 8:46:54 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 attempt registry request try #3 at 8:48:34 AM
npm http request GET https://registry.npmjs.org/express
npm ERR! Linux 4.13.0-36-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v7.10.1
npm ERR! npm v4.2.0
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! /root/.npm/_logs/2018-03-02T08_49_14_320Z-debug.log
The command '/bin/sh -c npm install' returned a non-zero code: 1
根据stackoverflow中的许多文章,
1)我在Ubuntu的etc / hosts中进行了如下更改:
127.0.0.1 localhost
127.0.1.1 ubuntu-VirtualBox
151.101.16.162 registry.npmjs.org
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
2)我还在/ etc / default / docker中输入:
DOCKER_OPTS="--dns 151.101.16.162"
它在docker build中不起作用。我该如何处理纠正它的步骤?
答案
请按照以下步骤纠正上面发生的Ubuntu中的错误:
1)使用ifconfig找出以太网的ip。
enp0s4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet **10.X.X.XX** netmask 255.255.255.0 broadcast 10.8.89.898
2)创建新网络并在绑定容器端口时启用默认IP并提及上述IP地址。
docker network create -o "com.docker.network.bridge.host_binding_ipv4"="10.X.X.XX" my-network
3)使用这个新创建的网络来构建docker镜像:
docker build --network my-network -t hello-world .
以上是关于npm安装在docker build中的主要内容,如果未能解决你的问题,请参考以下文章
docker build + 私有 NPM(+ 私有 docker hub)
npm install 后 Gitlab Shared Runner docker build
docker build 中的本地 npm 依赖“不包含 package.json 文件”,但使用 npm start 运行良好