As Ralf said in your comments, "localhost" from inside your container refers to the container itself.
An option to get around this is to also run MongoDB in a container, and then use docker-compose to run both containers and have them talk to each other.
For example, your (stripped-down, incomplete) docker-compose.yml file would look something like:
version: "3.3"
services:
mongo:
image: mongo:4.0.11-xenial
ports:
- 27017:27017
your-app:
# settings dependant on whatever your app is
depends_on:
- mongo
Then when you are trying to connect via your application, you can connect using mongodb://<user>:<pass>@mongo/YourDatabase, since Docker will map mongo onto your mongo service.
localhostis the container, not your docker host. Check out this post on one possibel way to connect to the Docker host: forums.docker.com/t/…