I’m currently facing an issue with communication between two containers in the same Docker network. One container is running Webserver.js, and the other one hosts MongoDB. Unfortunately, they don’t seem to be communicating, the console always prints the error: connect ECONNREFUSED 127.0.0.1:27017.
What puzzles me is that I explicitly specify the IP address of the container, but get an issue with 127.0.0.1 (localhost) mentioned, why ? Here’s an example of what I’m trying:
var url_db = 'mongodb://admin:admin@mongodb:27017/thal';
var url_db = 'mongodb://admin:admin@172.18.0.11:27017/thal';
If anyone could help me identify a solution to this problem, it would be a fantastic Christmas present!
Based on that you tried with a static IP address and the fact that you shared nothing Docker-related, but you shared a probably nodejs code, are you sure it is a Docker issue?
I understand that the webserver runs in Docker a container, but since it didn’t matter when you replaced the placeholder with an IP, it looks like that config is not used at all.
Regarding docker, there are just two containers built in the docker compose yml
Looks to be a Docker issue because, when I run both side in my Debian terminal (with local host), everything works.
The two lines above are extracted from my WebServer.js that runs in a container and tries to connect to another one.
But the error message is related to local host… strange
I tried most solutions on Forum.
Can you share the compose file and the code that actually connects to the database using the variable? People often don’t realize how much that could matter and help to understand the issue or trying to find the issue in another direction.
That’s the point. You had only one environment and now you had to create two. There could be mistakes in the process.
In my experience, when an error message says “A” while you expect “B”, the answer is not “B was magically converted to A”, but you miss something and “A” was always there.
Docker will not do anything with your config so your next job seems to be to find out where that localhost is coming from. And that is unlikely to be coming from Docker directly. It could be any kind of cache or even a built-in behavior to always connect to localhost and it just worked on localhost because it would have been conneced to that anyway.
Thank you, but it shows only the variables, not where the connection actually happens
Just to give you an workaround in case you can’t solve the original issue soon, you can share the network namespaces between the containers. Just the relevant parts of the compose file showing the network mode
Why do you use fixed IP addresses? Docker provides IP management and an internal DNS server. Usually it works very well and fully automatic, just access the other service in the Docker network by the service name.
And for plain NodeJS debugging, maybe try
log.info("Connecting to mongo", config.DB_CONN_STRING);
I have 6 containers in all the project that are communicating each other in the same network but with just their name, it doesn’t work. IP addr are working well except for this case.
2024-01-04 09:35:40.965 INFO [dist/core/Database.js:36 Function.<anonymous>] Connecting to mongo database...
/opt/app/node_modules/mongodb/lib/sdam/topology.js:292
const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);
^
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27057
at Timeout._onTimeout (/opt/app/node_modules/mongodb/lib/sdam/topology.js:292:38)
I don’t see the DN_CONN_STRING in log.info. Looks like it’s not taken into account.
When using another container’s network namespace, ports must published on the container that uses the bridge network and not another container’s network namespace. It was just an alternative option though. I totally get that you prefer not to share network namespaces.