I have a similar problem discussed in Cannot connect to MongoDB in docker.

  • Windows host
  • Docker Toolbox, so it is using VirtualBox

I used docker run --name mongo2 -p mongo to create the MongoDB container.

When I connect to the container (docker -it mongo2 /bin/bash), I can connect to Mongo via mongo localhost:27017.

I also checked with netstat that Mongo is listening on, not only localhost.

Still, I'm not able to connect from my Windows host to the container. I even tried to disable Windows firewall and still nothing:

rs@ausus:~$ telnet localhost 27017
telnet: Unable to connect to remote host: Connection refused

Any ideas?

  • 2
    Problem found & solved. When I opened the boot2docker VM, I saw that it is listening on So I created the container without specifying the interface, only "docker run --name mongo1 -p 27017:27017 mongo" and then the boot2docker VM was listening on all interfaces. Then I needed to get the address of the VM ("docker-machine env") and voila, from the Windows host PC I can "mongo" Commented Aug 6, 2017 at 15:22

3 Answers 3


The answer is provided in the first comment actually (and it solved for me the same problem), but it is easier to find it when it is in the Answer section.

The answer will assume that you're using Docker Toolbox.

Instead of running:

docker run --name mongo2 -p mongo

the following command should be run:

docker run --name mongo2 -p <ip of boot2docker VM>:27017:27017 mongo

To get the <ip of boot2docker VM> VM:

  1. Go to VirtualBox --> select "default" VM --> click "Show" button.
  2. Run the command: docker-machine env.
  3. The IP of the VM will be in the output.

Like the Docker documentation says at https://hub.docker.com/_/mongo/ : "This means that it is not possible to run a MongoDB container with the data directory mapped to the host"

Finally I have done a .bat script that defines my Win10 IP and map it to a variable. I run MongoDB directly on my Win10 and then access it with the IP. That is not what I want but it works.

startup.bat :

for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set DOCKERHOST=%ip:~1%
docker-compose up

docker-compose.yml :


Use this:


This is enabled on recent versions of Docker Desktop for Windows.

To test, you can also ping the Docker network interface:

C:\>ping host.docker.internal
Pinging host.docker.internal [] with 32 bytes of data:
Reply from bytes=32 time<1ms TTL=128

Quote from docs:

The host has a changing IP address (or none if you have no network access). We recommend that you connect to the special DNS name host.docker.internal which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows.

The "will not work in a production environment" sounds like a big downside, but if this Linux-based container is deployed to the cloud under Linux then the DNS name will change anyway, so it's a moot point.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .