Reputation: 2086
Hi i have a image created by below Dockerfile
# This dockerfile uses the gabrielwu/metinvest image
# Base image to use, this must be set as the first line
FROM gabrielwu/mongodb:v0.01
# Maintainer: docker_user <docker_user at email.com> (@docker_user)
MAINTAINER gabrielwu
EXPOSE 27017
EXPOSE 28017
# Commands to update the image
# Commands when creating a new container
#CMD ["-f /home/mongodir/conf/mongod.conf"]
ENTRYPOINT mongod -f /home/mongodir/conf/mongod.conf
And i created a container based on above image. below are commands i used to create the container. And i tried to connect it from outside with mongo client. It said connection refused.
1010deiMac:metinvest whoami$ docker run -d -p 127.0.0.1:27017:27017 -p 127.0.0.1:28017:28017 gabrielwu/mongodb:v0.02
4c6352575be95c0162a9603ed702d51a8cadcca505c7744b6fac19b84b33a920
1010deiMac:metinvest whoami$ mong
-bash: mong: command not found
1010deiMac:metinvest whoami$ mongo
MongoDB shell version: 3.0.6
connecting to: test
2016-09-15T21:37:51.831+0800 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2016-09-15T21:37:51.833+0800 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:179:14)
at (connect):1:6 at src/mongo/shell/mongo.js:179
exception: connect failed
1010deiMac:metinvest whoami$
Also this is logs i see for the container. You can see mongodb is running pretty within container.
1010deiMac:metinvest whoami$ docker logs 4c6352575be95c0162a9603ed702d51a8cadcca505c7744b6fac19b84b33a920
Thu Sep 15 13:22:29.694 [initandlisten] MongoDB starting : pid=5 port=27017 dbpath=/home/mongodir/data 64-bit host=4c6352575be9
Thu Sep 15 13:22:29.694 [initandlisten] db version v2.4.9
Thu Sep 15 13:22:29.695 [initandlisten] git version: nogitversion
Thu Sep 15 13:22:29.695 [initandlisten] build info: Linux orlo 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 17:37:58 UTC 2013 x86_64 BOOST_LIB_VERSION=1_54
Thu Sep 15 13:22:29.695 [initandlisten] allocator: tcmalloc
Thu Sep 15 13:22:29.695 [initandlisten] options: { config: "/home/mongodir/conf/mongod.conf", dbpath: "/home/mongodir/data" }
Thu Sep 15 13:22:29.699 [initandlisten] journal dir=/home/mongodir/data/journal
Thu Sep 15 13:22:29.700 [initandlisten] recover : no journal files present, no recovery needed
Thu Sep 15 13:22:30.221 [initandlisten] preallocateIsFaster=true 10.02
Thu Sep 15 13:22:30.373 [initandlisten] preallocateIsFaster=true 2.58
Thu Sep 15 13:22:31.492 [FileAllocator] allocating new datafile /home/mongodir/data/local.ns, filling with zeroes...
Thu Sep 15 13:22:31.492 [FileAllocator] creating directory /home/mongodir/data/_tmp
Thu Sep 15 13:22:31.497 [FileAllocator] done allocating datafile /home/mongodir/data/local.ns, size: 16MB, took 0.002 secs
Thu Sep 15 13:22:31.497 [FileAllocator] allocating new datafile /home/mongodir/data/local.0, filling with zeroes...
Thu Sep 15 13:22:31.500 [FileAllocator] done allocating datafile /home/mongodir/data/local.0, size: 64MB, took 0.002 secs
Thu Sep 15 13:22:31.501 [initandlisten] waiting for connections on port 27017
Thu Sep 15 13:22:31.501 [websvr] admin web console waiting for connections on port 28017
And i also checked with docker ps.
1010deiMac:metinvest whoami$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c6352575be9 gabrielwu/mongodb:v0.02 "/bin/sh -c 'mongod -" 30 minutes ago Up 14 minutes 127.0.0.1:27017->27017/tcp, 127.0.0.1:28017->28017/tcp cocky_mestorf
Now i'm totally lost why i couldn't connect to it from outside.
--------------------------Edit--------------------------------------
Now i removed the 127.0.0.1 when creating container and tried again. it ended failure as well.
1010deiMac:metinvest whoami$ docker run -d -p 27017:27017 -p 28017:28017 gabrielwu/mongodb:v0.02
3eb14d204370867cab18ed26a55ecb42719772852c691c5163d6906cbbb6a329
1010deiMac:metinvest whoami$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3eb14d204370 gabrielwu/mongodb:v0.02 "/bin/sh -c 'mongod -" 9 seconds ago Up 10 seconds 0.0.0.0:27017->27017/tcp, 0.0.0.0:28017->28017/tcp lonely_khorana
1010deiMac:metinvest whoami$ mongo
MongoDB shell version: 3.0.6
connecting to: test
2016-09-15T22:57:07.713+0800 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2016-09-15T22:57:07.715+0800 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:179:14)
at (connect):1:6 at src/mongo/shell/mongo.js:179
exception: connect failed
1010deiMac:metinvest whoami$
----------------------------Edit Update ------------------------
I tried it with ip from machine still can't work:
1010deiMac:metinvest whoami$ docker-machine env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/whoami/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(docker-machine env)
And
1010deiMac:metinvest whoami$ docker run -d -p 192.168.99.100:27017:27017 -p 192.168.99.100:28017:28017 gabrielwu/mongodb:v0.02
259cd941be6a005f2dcaf9474625fc423a1ba408d6321e56b5c0f517999abe62
And
1010deiMac:metinvest whoami$ mongo
MongoDB shell version: 3.0.6
connecting to: test
2016-09-15T23:40:33.170+0800 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2016-09-15T23:40:33.172+0800 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:179:14)
at (connect):1:6 at src/mongo/shell/mongo.js:179
exception: connect failed
Upvotes: 0
Views: 2161
Reputation: 2086
The problem i couldn't connect to my localhost host of PC to the container is because container is running in VM !!!
Here are some articles i love to share regarding this issue: Accessing docker containers on localhost when using Boot2Docker
port forwarding in docker machine
The tricky thing is to understand container is running in a vm so when expose a port of container it's actually mapping to the port in vm, not port in my PC localhost.
Here is how to fix the problem. 1: map ports of vm to localhost. See how to use this command, please refer to VBoxManage controlvm
VBoxManage controlvm default natpf1 "tcp-port27017,tcp,127.0.0.1,27017,,27017"
VBoxManage controlvm default natpf1 "tcp-port28017,tcp,127.0.0.1,28017,,28017"
you also may need to query if the mappings are added, or you want to further check, check this LIST natpf rules in Virtualbox/Vagrant
Well i prefer to use showvminfo to check mappings. You can find your mappings in its output.
VBoxManage showvminfo default
then start up your container, and test with mongo client:
1010deiMac:metinvest whoami$ docker run -d -p 27017:27017 -p 28017:28017 gabrielwu/mongodb:v0.02
94d1a4639d662b3ec04a2f363efb984b2d1ff9c4df1db1026d6c94f86013de26
1010deiMac:metinvest whoami$ mongo
MongoDB shell version: 3.0.6
connecting to: test
>
it works.
Upvotes: 1