How to access local MongoDB server from a Docker Environment

It is actually pretty easy. In my case I needed to reach from Laravel Sail to my local MongoDB Database.

All I needed to do is change DB_DSN in my .env file accordingly.

It was earlier:

DB_DSN=mongodb://localhost:27017/<your_database_name>

What it needs to be:

DB_DSN=mongodb://host.docker.internal:27017/<your_database_name>

You can find detailed information in Docker Docs under "I want to connect from a container to a service on the host" topic.

I hope it helps.

Leave a Reply

Your email address will not be published. Required fields are marked *