Connecting to mongo container using node getting MongoError
Thanks Nehal for the comment.
The answer lies here https://docs.docker.com/docker-for-mac/networking/#known-limitations-use-cases-and-workarounds
Basically, you cannot see a docker0 interface in macOS which means your unable to route traffic to containers. However, that can be solved using port forwarding.
All I have to change is the docker run command:
docker run -d -p 27017:27017 --name my-mongo mongo
This means that you expose the Linux port, which is docker in this instance, and forward it to your Mac (-p
). The -d
flag runs the process in the background.
Then mongoose.connect("mongodb://localhost:27017/test");
should work.