Skip to content

Node App not connecting when run through Docker Compose [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

After building and uploading an image of the app to DockerHub, I tried running it via docker compose and encountered a completely blank localhost:3000 page. The mongodb and mongo-express pages work normally, and running docker logs on the myapp container provided the following info :

UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

I modified the docker-compose.yaml file in order to ensure the mongo containers start before our app, along with making the neccesary changes required for docker container deployment.

The files are attached here : docker-compose.yaml server.js

I tried following the steps across various stackoverflow issues describing the same, but none of them could provide a solution.

Any help would be greatly appreciated!

Edited by Abhishek Singh Dhadwal