Alexander Mills
Alexander Mills

Reputation: 99959

Cannot connect to MongoDB running in local Docker container

I have this command to start MongoDB in a container:

docker run -p 27017:27017 --name cdt-mongo mongo

this is from these docs:

https://hub.docker.com/_/mongo/

I actually think the

-p 27017:27017

is superfluous. anyway.

Locally, I run the container in one terminal, and then in another terminal I issue the mongo command, and I get:

$ mongo
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017
2017-05-02T11:27:22.864-0700 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-05-02T11:27:22.866-0700 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed

anybody know why I cannot connect to mongodb running in the container?

Upvotes: 0

Views: 1783

Answers (1)

Alexander Mills
Alexander Mills

Reputation: 99959

because I am on MacOS, I should not connect to

localhost:27017

but instead use the host/ip address of the docker-machine VM

HOST=$(docker-machine ip)

and then connect to it like so:

mongo --host $HOST

Upvotes: 1

Related Questions