1

I am trying to connect to docker mongo image in my local windows machine, I installed mongodb in local, and added the mongodb bin path to windows path environment variable. And I am doing below steps

cd <docker-location>

docker login

docker ps

-- to check already mongo running

docker pull myrepo/mymongo-image:1.0
docker run -p 27017:27017 -d --net=host --name=mytestDB myrepo/mymongo-image:1.0

docker logs mytestDB 

Output: MongoDB starting : pid=6 port=27017 dbpath=/data/db 64-bit host=moby docker ps Output:

CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS               NAMES
34598734598        myrepo/mymongo-image:1.0   "/bin/sh -c /usr/bin/"   12 hours ago        Up About a minute                       mytestDB

start mongo in local:

mongo --port 27017

But I am getting error like this:

MongoDB shell version: 3.2.1
connecting to: 127.0.0.1:27017/test
2016-10-13T20:04:12.273+0530 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made because the target machine actively refused it.
2016-10-13T20:04:12.277+0530 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:226:14
@(connect):1:6

exception: connect failed

Please let me know where I did mistake.

2

Don't use --net=host. It looks like you are asking docker to publish the port since you have supplied the -p 27017:27017 option, but --net=host asks Docker to turn off all network isolation features, including port publishing.

Docker-for-windows sets up a little VM in hyperv where Docker runs. Your --net=host container ends up listening on that VM's interfaces. The Docker-for-windows network port forwarding pays attention only to published ports, so that's why no forwarding is happening.

If your run that same command without --net=host, you should get a different result.

Sometimes, if you try to run a containerized process and the published port doesn't seem to be working, it is because the process is listening on the container's lo0 interface and not the eth0 interface.

Here's a good benchmark test container to run to test out publishing ports:

docker run --name testweb -d -p 8989:80 nginx:alpine

You should then be able to connect to http://127.0.0.1:8989 and see an nginx welcome page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.