"ECONNREFUSED" error after containering Node.js (Express) "ECONNREFUSED" error after containering Node.js (Express) express express

"ECONNREFUSED" error after containering Node.js (Express)


The Problem
From the container's point of view, 127.0.0.1 is the ip address of itself -- not the host OS. That means you shouldn't set 127.0.0.1:8080 as a HTTP_PROXY and HTTPS_PROXY because your container would be calling itself so it won't reach the Internet. This is why your npm install isn't working.

Similarly, your main application behind the node.js proxy should not be using

...ENV HTTP_PROXY "http://127.0.0.1:8446"ENV HTTPS_PROXY "https://127.0.0.1:8446"...

because that would be calling itself at port 8446 and not the 8446 of the host OS (which you intended to route to the other container running the node.js proxy, but this will never work).

The Solution
You have to use something like docker compose or docker swarm to link the two containers' network. Refer to the following example docker-compose.yml:

version: "3.7"services:  proxy:    image: myproxy    port:      - 8080:8080  app:    image: myapp

Also, remove the following lines from your proxy dockerfile and rebuild the image.

ENV HTTP_PROXY "http://127.0.0.1:8080"ENV HTTPS_PROXY "https://127.0.0.1:8080"

Similarly, change the main application dockerfile from this

ENV HTTP_PROXY "http://127.0.0.1:8446"ENV HTTPS_PROXY "https://127.0.0.1:8446"

to

ENV HTTP_PROXY "http://proxy:8446"ENV HTTPS_PROXY "https://proxy:8446"

Now run docker-compose up with this configuration, and your main app will be able to reach proxy container by the host name proxy rather than 127.0.0.1. Which means that you will use proxy:8080 to use the proxy running on port 8080.

PS: You are able to route to the docker containers/services via their service name because docker has a service discovery mechanism it maintains internally and it will resolve the ip address of these conatiners dynamically. This is essential to containers because containers can be destroyed and recreated at any time which means that IP addresses can change at any time. In order to resolve this, docker maintains a key-value store which maps service/host names to the IP addresses of these containers and resolve them for containers that are trying to reach other containers. Make sure to change all the IP addresses within your app to use host/service names instead of static IP addresses if they should route to other docker containers/services.