If you specified the correct port and still not able to connect to mongodb running in docker (like me), make sure you are using the service name (or container name) in your connection URL, e.g. mongodb://mongodb_service:27017/mydb
, which is defined in your docker-compose.yml
:
services:
mongodb_service:
image: mongo
I was using the hostname
value and that’s not the correct thing to do. You could verify this by looking at docker inspect mongodb_service
in the Aliases
section.