M HYPE SPLASH
// news

Docker build failed on npm install, reason: connect ECONNREFUSED 104.16.17.35:443

By Michael Henderson

I'm having a hard problem getting my Docker container up, it shows some weird error in the console:

Dockerfile:

FROM node:14
WORKDIR /usr/src/app/
COPY package.json package.json
COPY server.js server.js
RUN ping -c 4 google.com
RUN npm config set registry
RUN echo "${http_proxy}" && echo "${HTTP_PROXY}"
RUN npm install
# COPY . .
EXPOSE 3000
CMD ['npm', 'start']

When I run the docker build command

$ docker build -t myapp .

The error I'm getting:

Sending build context to Docker daemon 947.7kB
Step 1/7 : FROM node:14 ---> 7bef16bb2cf1
Step 2/7 : WORKDIR /usr/src/app/ ---> Using cache ---> 90402606c386
Step 3/7 : COPY package.json ./ ---> Using cache ---> b839b81ee876
Step 4/7 : RUN npm install ---> Running in 64378581f715
npm ERR! code ECONNREFUSED
npm ERR! errno ECONNREFUSED
npm ERR! FetchError: request to failed, reason: connect ECONNREFUSED 104.16.18.35:443
npm ERR! at ClientRequest.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-fetch-npm/src/index.js:68:14)
npm ERR! at ClientRequest.emit (events.js:315:20)
npm ERR! at TLSSocket.socketErrorListener (_http_client.js:469:9)
npm ERR! at TLSSocket.emit (events.js:315:20)
npm ERR! at emitErrorNT (internal/streams/destroy.js:106:8)
npm ERR! at emitErrorCloseNT (internal/streams/destroy.js:74:3)
npm ERR! at processTicksAndRejections (internal/process/task_queues.js:80:21)
npm ERR! FetchError: request to failed, reason: connect ECONNREFUSED 104.16.18.35:443
npm ERR! at ClientRequest.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-fetch-npm/src/index.js:68:14)
npm ERR! at ClientRequest.emit (events.js:315:20)
npm ERR! at TLSSocket.socketErrorListener (_http_client.js:469:9)
npm ERR! at TLSSocket.emit (events.js:315:20)
npm ERR! at emitErrorNT (internal/streams/destroy.js:106:8)
npm ERR! at emitErrorCloseNT (internal/streams/destroy.js:74:3)
npm ERR! at processTicksAndRejections (internal/process/task_queues.js:80:21) {
npm ERR! type: 'system',
npm ERR! errno: 'ECONNREFUSED',
npm ERR! code: 'ECONNREFUSED'
npm ERR! }
npm ERR!
npm ERR! If you are behind a proxy, please make sure that the
npm ERR! 'proxy' config is set properly. See: 'npm help config'
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-03-02T08_20_08_546Z-debug.log
The command '/bin/sh -c npm install' returned a non-zero code: 1

I'm on Ubuntu 20.4 and it is just a simple node app with a server.js file. let me know if you need anything else from me to get it fixed. Thank you in advance for your help!

1 Answer

It could be an error handling the secure connection to registry.npmjs.org

Also, the steps shown in the error output don't match your Dockerfile (eg : missing the output of RUN ping -c 4 google.com), maybe you have edited your files afterward to check if dns resolving is working, and I believe so because the npm install error display the server ip.

To better identify the error, you can add a new step in your Dockerfile (before RUN npm install) :

RUN curl -v 

Then run your build command again, curl will try to connect to the url and -v flag will output a lot of details, including the tls handshake. It will help understand what is really happening.

Side notes :

  • You can add --no-cache to the docker build command, it will force docker to execute every command from your Dockerfile (and not only the commands that come after a new/updated command) : docker build --no-cache -t myapp . A fresh build could be sometimes necessary.
  • You can add --pull to the docker build command, it will force docker to get the latest version of your base image : docker build --pull -t myapp . Usefull when you know the base has been updated.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy