10

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:443. The same work with docker 1.91 on ubuntu 14.04. When I get bash and install inetutils-ping on container I noticed I can't ping anywhere

root@9deb4b274c1e:/home/nodexp#ping 8.8.8.8           
PING 8.8.8.8 (8.8.8.8): 56 data bytes
^C--- 8.8.8.8 ping statistics ---
4 packets transmitted, 0 packets received, 100% packet loss

Why is that ? Of course I can ping from RHEL

Dockerfile

FROM node:argon
# Create user nodexp in group nodexp
RUN groupadd -r nodexp \
    && useradd -m -r -g nodexp nodexp
WORKDIR /home/nodexp
# Install app dependencies
COPY package.json /home/nodexp
RUN npm install
# Bundle app source
COPY . /home/nodexp
EXPOSE 3000
CMD [ "npm", "start" ]

and package.json

{
  "name": "mp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "~4.13.1"

  }
}
irom
  • 3,316
  • 14
  • 54
  • 86

7 Answers7

19

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

sudo service docker restart

gafi
  • 12,113
  • 2
  • 30
  • 32
  • has this problem, rebooted server and also fixed, yours answer migth have worked too – PPP Mar 15 '20 at 02:36
16

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
Aditya Kresna Permana
  • 11,869
  • 8
  • 42
  • 48
2

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.

vegasbrianc
  • 989
  • 1
  • 7
  • 11
  • 2
    Oh my gosh! Your solution worked for me! Let me explain my case here, in comments, maybe it'll save someone's time! I have a Jenkins job that builds an image based on Node.JS image, I also use private registry which is in the same network with the Jenkins, but the job could not use Docker's network by default for some reason. After adding this `--network` flag the job stopped to fail with discovery npm registry. Thank you! – Egor Sazanovich Jul 01 '20 at 22:53
  • Super happy it helped someone. – vegasbrianc Jul 03 '20 at 07:37
1

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.

Hans Poo
  • 804
  • 1
  • 8
  • 17
0

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

hb0
  • 3,350
  • 3
  • 30
  • 48
0

I was following a tutorial. This error doesn't happen for me when I copy all my files:

# Specify base image
FROM node:alpine

WORKDIR /usr/app

# Install dependencies
COPY ./ ./
RUN npm install

# Default command
CMD ["npm", "start"]

It happens with me when I copy package.json first and then copy other files after npm install:

# Specify base image
FROM node:alpine

WORKDIR /usr/app

# Install dependencies
COPY ./package.json ./
RUN npm install

COPY ./ ./

# Default command
CMD ["npm", "start"]
Ashish Gaur
  • 2,030
  • 2
  • 18
  • 32
0

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"]
Rewanth Tammana
  • 1,453
  • 17
  • 20
  • This results in: ERROR: unsatisfiable constraints: ca-certificates (missing): required by: world[ca-certificates] The command '/bin/sh -c apk add --no-cache ca-certificates' returned a non-zero code: 1 – J. S. Nov 19 '20 at 11:53