0

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?

8
  • 1
    Firstly, you have -d and --rm, and according to the docs, this is not allowed. Commented May 2, 2017 at 18:32
  • I can not reproduce with your commands, what is the docker logs containerId output? Commented May 2, 2017 at 18:34
  • Have you checked the mongo container's logs, to see if it is running smoothly or not? Commented May 2, 2017 at 18:36
  • @MattClark thanks, good catch, but that's not the root problem Commented May 2, 2017 at 18:54
  • Thanks all, even if it's not running as a daemon, I have the same problem. I have removed both the -d and -rm flags. Same problem persists. When I run the container without daemon flag, I get "2017-05-02T18:38:24.665+0000 I NETWORK [thread1] waiting for connections on port 27017"....so it should be listening on the expected port right? Commented May 2, 2017 at 18:55

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

just an observation, you don't need to use docker-machine anymore, you can use the native docker, so you would be able to use localhost
thanks, that would be great - do you have a link to that? I don't how to find info on how to use Docker on MacOS w/o docker-machine.
If you go to their official website docker.com/community-edition all tutorials will now be using the native Docker, as it's not Beta anymore

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.