Your setup looks correct.
Connection string that you should use is:
mongodb://mongodb:27017
If you have still issues, please dobule check:
- Are you running your solution by docker-compose up ?? Only docker-compose will work since docker-compose creates network and DNS resolution between services.
- Is your mongodb service really running after docker-compose up? Are there any logs in this container? Maybe there is a problem with this container, it goes done and that's why database is unavailable.