0

I am working on a Rest API and trying to connect to a MongoDb Docker container. I am on a Windows 10 machine and managed to get Docker up and running using Bash and Oracle Virtualbox VM. I ran $ docker run --rm --name mongo -d -p 27017:27017 -v mongodbdata:/data/db mongo:4.0.4 in Bash.
I ran docker inspect and the ports are exposed as follows:

"NetworkMode": "default",
        "PortBindings": {
            "27017/tcp": [
                {
                    "HostIp": "",
                    "HostPort": "27017

In network settings I have

NetworkSettings": {
        "Bridge": "",
        "SandboxID": "f7d140fc6f524c8e854ced371c19ff4661f899bc96a42f7e817b301a9a514887",  
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "27017/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "27017"
                }
            ]
        },
        "SandboxKey": "/var/run/docker/netns/f7d140fc6f52",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "EndpointID": "fafa4bd9c05743874fa6783fce5b40c74cea0032cf51f35182efdc5eafd3156e", 
        "Gateway": "172.17.0.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "172.17.0.2"

 

I am using c# Visual Studio Code and try to connect to the MongoDb Extension with the connection mongodb://locahost:27017 but I am getting ERRCONN REFUSED. I have tried using all combos of Mongodb:// localhost, 127.0.0.1, 172.17.0.2 and 0.0.0.0 but no luck. Also when I run the API I am getting "port 27017" actively refused the connection".
I am concerned there is some sort of conflict between Windows10, Bash, VS Code and Docker and they can't talk to each other. Any help would be much appreciated.

1
  • Try using exactly the same as docker. The extra setting may be causing issues. Commented Oct 13, 2022 at 11:20

2 Answers 2

0

Looking through some of the other answers here, I found out if you run "docker-machine ls" you get the IP of the VM running docker and that's the IP you need to use if using VirtualBox. Connected to Mongo straight away. Cheers!

Sign up to request clarification or add additional context in comments.

Comments

0

I was able to connect to a container from the host machine using the following string:

mongodb://127.0.0.1:27017/?authSource=admin&readPreference=primaryPreferred&retryWrites=false&directConnection=true

the directConnection=true is what helped me. Hope this helps you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.