docker npm install Error: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443

前端 未结 7 1369
Happy的楠姐
Happy的楠姐 2020-12-16 23:48

I\'m using docker version 1.10.1 on RHEL 7 and getting npm install error when using below Dockerfile. The Error: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:

相关标签:
7条回答
  • 2020-12-17 00:01

    I ran into this same issue. My workaround was to attach docker build to a known working and accessible Docker network.

    Workaround

    • docker network ls
    • Choose a known working network
    • docker build --network=<known working network name>

    By default, Docker uses the default network for building. Setting the network manually ensures the network can access the internet.

    0 讨论(0)
  • 2020-12-17 00:01

    If you are able to ping/curl to npm registry but still getting error with npm install, the error might likely be due to missing ca-certs package. Add the following line to Dockerfile before npm install and re-build the image.

    FROM node:alpine
    
    # Installing cert package will allow resolving the error to https://registry.npmjs.org/
    RUN apk add --no-cache ca-certificates
    
    RUN npm install
    CMD ["npm", "start"]
    
    0 讨论(0)
  • 2020-12-17 00:08

    I fix this problem based on this article https://development.robinwinslow.uk/2016/06/23/fix-docker-networking-dns

    actually, you could check the DNS if it fails or not while calling registry.npmjs.org

    To check that thing, I did these steps to make it work

    Step 1

    Run this command on busybox image, I will use google.com to simulate the connection request

    >> docker run busybox nslookup google.com 
       Unable to find image 'busybox:latest' locally
       latest: Pulling from library/busybox
       f70adabe43c0: Pull complete 
       Digest: sha256:58ac43b2cc92c687a32c8be6278e50a063579655fe3090125dcb2af0ff9e1a64
       Status: Downloaded newer image for busybox:latest
       nslookup: can't resolve 'google.com'
       Server:    8.8.8.8
       Address 1: 8.8.8.8
    

    Step 2

    As you can see from step 1 result, I got an error and cannot resolve connection to google.com, if you got a similar error then do this to check your current DNS route.

     >> nmcli dev show | grep 'IP4.DNS' 
        IP4.DNS[1]:                             192.168.2.1
    

    That command exposes your IP4 DNS which in this case 192.168.2.1, on this step you already know the DNS.

    Step 3

    Let's continue using busybox container to connect using this DNS by running

     >> docker run --dns 192.168.2.1 busybox nslookup google.com
        Server:    192.168.2.1
        Address 1: 192.168.2.1 giftcard.dyndns.org
    
        Name:      google.com
        Address 1: 2404:6800:4003:c03::65
        ....
    

    Step 4

    If your result is similar like on step 3, then your problem is docker couldn't connect because docker doesn't know the DNS will be used on, so we fix that by making daemon.json file and locate that in /etc/docker/daemon.json. these the content to put.

    {
      "dns": ["192.168.2.1", "8.8.8.8"] // 192.168.2.1 is value of your dns
    }
    

    Step 5

    Restart your docker service

    >> sudo service docker restart
    
    0 讨论(0)
  • 2020-12-17 00:11

    restarting docker with this command fixes it for me but I don't know why

    sudo service docker restart

    0 讨论(0)
  • 2020-12-17 00:17

    For me the problem was, that my swap-partition had problems after resizing my root partition and asked me for my passphrase for almost all system tasks (like apt get commands, re-/booting, etc.).

    Please enter passphrase for disk … (cryptswap1) on none

    Note

    Only try this, if you are seeing the error above, too, and experience the problem described in the question when you try to execute npm install inside a docker container:

    Error: getaddrinfo ENOTFOUND/EAI_AGAIN registry.npmjs.org registry.npmjs.org:443

    The Solution

    1. Open GParted and check which where your swap partition is mounted, e.g. /dev/sda5 (referenced as "SWAP_PARTITION")
    2. Edit /etc/crypttab and replace UUID=**** to get the following pattern:

      cryptswap1 /dev/SWAP_PARTITION/ /dev/urandom swap,**,cipher=****

    3. Reboot, if you are still asked for your passphrase, continue:

    4. Execute the command sudo dd if=/dev/zero of=/dev/SWAP_PARTITION/ bs=512 count=20480 and reboot. This fixed it for me.

    source

    0 讨论(0)
  • 2020-12-17 00:18

    I tried everything above and nothing worked for me. A curiosity dockerd systemd script didn't start when adding the --dns parameter.

    In my case the problem was that in ubuntu /etc/resolv.conf is generated automatically by systemd-resolved and points to a local ip 127.0.0.53, where a cache DNS is running, is crazy but with this information the containers where trying to resolve DNS with themselves in its loopack interface. Changing resolv.conf manually to our company DNS in the LAN fixed the issue, then modified systemd-resolved to do it permanently.

    0 讨论(0)
提交回复
热议问题