简体   繁体   中英

Unable to connect to mongodb in docker container

Getting connection refused when trying to connect to MongoDb in Docker container.

  1. Here's the output of docker ps:

docker ps CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES 21063296d7d8 mongo "/entrypoint.sh mongo"
44 minutes ago Up 29 minutes 0.0.0.0:57017->27017/tcp
mongodb-dev

  1. Here's the snippet of docker logs mongodb-dev:

2015-11-28T05:36:36.363+0000 I CONTROL [initandlisten] db version v3.0.7 2015-11-28T05:36:36.363+0000 I CONTROL [initandlisten] git version: 6ce7cbe8c6b899552dadd907604559806aa2e9bd 2015-11-28T05:36:36.363+0000 I CONTROL [initandlisten] build info: Linux ip-10-183-78-195 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64 BOOST_LIB_VERSION=1_49 2015-11-28T05:36:36.363+0000 I CONTROL [initandlisten] allocator: tcmalloc 2015-11-28T05:36:36.363+0000 I CONTROL [initandlisten] options: {} 2015-11-28T05:36:36.365+0000 I NETWORK [initandlisten] waiting for connections on port 27017

  1. Here's the error when connecting to MongoDB in docker using port 57017

mongo localhost:57017

MongoDB shell version: 3.0.7 connecting to: localhost:57017/test 2015-11-27T22:09:22.733-0800 W NETWORK Failed to connect to 127.0.0.1:57017, reason: errno:61 Connection refused 2015-11-27T22:09:22.734-0800 E QUERY Error: couldn't connect to server localhost:57017 (127.0.0.1), connection attempt failed at connect (src/mongo/shell/mongo.js:181:14) at (connect):1:6 at src/mongo/shell/mongo.js:181 exception: connect failed

Please let me know what I'm missing here. Thank you for your help!

It depends if you are using docker directly on a Linux host or on a Mac.
In the latter case, you would need to make sure you have port forwarded 57017 in your VirtualBox connection settings, as I mention in " How to connect mysql workbench to running mysql inside docker? "

VBoxManage controlvm "boot2docker-vm" --natpf1 "tcp-port57017 ,tcp,,57017,,57017"
VBoxManage controlvm "boot2docker-vm" --natpf1 "udp-port57017 ,udp,,57017,,57017"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM