How can my application (in a Docker container) access MongoDb (on my host)?

Viewed 350

I am using Visual Studio 2019 Community edition and I have an ASP.NET CORE 3.1 application.

  • In Visual Studio I am running and debugging (F5) the application in a Docker container (linux).
  • The application is supposed to access a MongoDb database.
  • The MongoDb engine is installed directly on my host machine (default port 27017).
  • In the connection string I am using server 127.0.0.1 and port 27017.

So, my application runs inside a Docker container, but the database runs on my host system (MS Windows).

My problem:

As soon as the application tries to access the database, I am getting a timeout exception. I guess, I am getting the exception because the application can't reach out to my host system, right?

How can I allow my container application to access host applications like MongoDb?

Can I open "outgoing ports" in the "Dockerfile"? Or is 127.0.0.1 the wrong address for my host system?

I appreciate any help. Thx guys!

1 Answers

Ok, I got is solved on my own.

Instead of "127.0.0.1" or "localhost" I had to use "host.docker.internal" in my connection string.

Related