connecting to local mongodb from docker container connecting to local mongodb from docker container docker docker

connecting to local mongodb from docker container


On Docker for Mac, you can use host.docker.internal if your mongo is running on your localhost. You could have your code read in an env variable for the mongo host and set it in the Dockerfile like so:

ENV MONGO_HOST "host.docker.internal"

See here for more details on https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds


Adding @vlad-holubiev as answer here because it has worked for me and help users to find it.

Using the network host option on docker run, as specified in the docs:

With the network set to host a container will share the host’s network stack and all interfaces from the host will be available to the container. The container’s hostname will match the hostname on the host system.

docker run -d -e ROOT_URL=http://localhost -e MONGO_URL=mongodb://localhost:27017 --network="host"


A docker container is a separate from your computer,
So you will not be able to connect to your localhost.
You should run the mongo server on a container and the connect with the image name.

mongodb://CONTAINER-NAME:port