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

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

sudo service docker restart


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

Tags:

Docker

Node.Js