代码之家  ›  专栏  ›  技术社区  ›  Arshanvit

npm在docker build内部安装

  •  1
  • Arshanvit  · 技术社区  · 6 年前

    我是Dockering a nodejs 应用于 ubuntu 17.10 本地使用Oracle VM Virtualbox。

    我在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中不起作用。 我如何继续执行纠正步骤?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Arshanvit    6 年前

    请按照以下步骤更正上述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 .