无法从外部

时间:2016-09-15 13:45:58

标签: docker dockerfile

您好我有一个由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

我根据上面的图像创建了一个容器。下面是我用来创建容器的命令。我试图从外部与mongo客户端连接。它说连接被拒绝了。

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$ 

这也是我看到容器的日志。你可以看到mongodb在容器内运行良好。

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

我还用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

现在我完全迷失了为什么我无法从外面连接它。

--------------------------编辑-------------------- ------------------

现在我在创建容器时删除了127.0.0.1并再次尝试。它也结束了失败。

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$ 

----------------------------编辑更新----------------- -------

我用机器上的ip尝试它仍然无法正常工作:

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)

并且

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

并且

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

1 个答案:

答案 0 :(得分:1)

我无法连接到我的本地主机PC到容器的问题是因为容器在VM中运行!!!

以下是我喜欢分享的有关此问题的一些文章:Accessing docker containers on localhost when using Boot2Docker

port forwarding in docker machine

棘手的是要理解容器是在vm中运行的,所以当暴露容器的端口时,它实际上映射到vm中的端口,而不是我的PC localhost中的端口。

以下是解决问题的方法。 1:将vm的端口映射到localhost。有关如何使用此命令的信息,请参阅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"

您还可能需要查询是否添加了映射,或者您想进一步检查,请检查LIST natpf rules in Virtualbox/Vagrant

我更喜欢使用showvminfo来检查映射。您可以在其输出中找到您的映射。

VBoxManage showvminfo default

然后启动容器,并使用mongo客户端进行测试:

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
> 

它有效。