gpt4 book ai didi

node.js - 在 docker build 中安装 npm

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:38 26 4
gpt4 key购买 nike

我正在使用 Oracle VM Virtualbox 在本地 ubuntu 17.10 上 Docker 化一个 nodejs 应用程序。

我在 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 构建中不起作用。我该如何继续采取措施来纠正它?

最佳答案

请按照以下步骤纠正上述 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)创建一个新网络并启用绑定(bind)容器端口时的默认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 .

关于node.js - 在 docker build 中安装 npm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49065709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com