MongoDB on with Docker "failed to connect to server [localhost:27017] on first connect "

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

According to your docker-compose.yaml file you can access you mongo container on 127.0.0.1:27017 only from host machine. In order to access it from NodeJS backend container you should use db:27017.