簡體   English   中英

無法從外部連接到Docker容器

[英]can't connect to docker container from outside

嗨,我有一個由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

我無法將PC的本地主機連接到容器的問題是因為容器在VM中運行!

這是我喜歡分享的一些有關此問題的文章: 使用Boot2Docker時訪問localhost上的Docker容器

Docker機器中的端口轉發

棘手的事情是了解容器是否在vm中運行,因此當暴露容器的端口時,它實際上是映射到vm中的端口,而不是我的PC本地主機中的端口。

這是解決問題的方法。 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"

您可能還需要查詢是否添加了映射,或者要進一步檢查,請在Virtualbox / Vagrant中檢查此LIST natpf規則

好吧,我更喜歡使用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
> 

有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM