我无法从主机(macOS 10.14.5)连接到在Docker容器(Docker版本18.09.2,内部版本6247962)中运行的mongod
。我使用--network host
选项启动容器,并将mongod
设置为监听0.0.0.0
。
docker run --network host --name testmongo -it mongo:latest /bin/bash
root@linuxkit-025000000001:/# mongod --bind_ip 0.0.0.0
2019-06-07T00:28:56.227+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-06-07T00:28:56.232+0000 I CONTROL [initandlisten] MongoDB starting : pid=13 port=27017 dbpath=/data/db 64-bit host=linuxkit-025000000001
2019-06-07T00:28:56.232+0000 I CONTROL [initandlisten] db version v4.0.10
2019-06-07T00:28:56.232+0000 I CONTROL [initandlisten] git version: c389e7f69f637f7a1ac3cc9fae843b635f20b766
2019-06-07T00:28:56.232+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
2019-06-07T00:28:56.232+0000 I CONTROL [initandlisten] allocator: tcmalloc
2019-06-07T00:28:56.232+0000 I CONTROL [initandlisten] modules: none
2019-06-07T00:28:56.232+0000 I CONTROL [initandlisten] build environment:
2019-06-07T00:28:56.232+0000 I CONTROL [initandlisten] distmod: ubuntu1604
2019-06-07T00:28:56.232+0000 I CONTROL [initandlisten] distarch: x86_64
2019-06-07T00:28:56.232+0000 I CONTROL [initandlisten] target_arch: x86_64
2019-06-07T00:28:56.233+0000 I CONTROL [initandlisten] options: { net: { bindIp: "0.0.0.0" } }
我现在希望我可以在主机上访问localhost:27017
并连接到mongod
服务器,但是我一直收到Connection Refused
的响应。从jupyter console
并使用pymongo
:
In [1]: import pymongo
In [2]: client = pymongo.MongoClient(host='localhost', port=27017)
In [3]: client.list_database_names()
---------------------------------------------------------------------------
ServerSelectionTimeoutError Traceback (most recent call last)
<ipython-input-3-62f658703d98> in <module>
----> 1 client.list_database_names()
[LONG ERROR MESSAGE]
ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused
任何对我在这里做错事的见解都将受到赞赏。
答案 0 :(得分:2)
根据此link,network host
仅在Linux主机上有效,而在MacO的Docker Desktop上无效。
要解决此问题,请使用-p
之类的-p 27017:27017
选项运行docker
例如: docker run -d -p 27017:27017 -v ~/data:/data/db mongo