给出docker-compose的以下文件
version: '2'
services:
sut:
build: .
command: /bin/bash #nosetests
depends_on:
- mongo
mongo:
image: mongo
我尝试动态连接到mongo服务器。我对这个雄心勃勃的计划并没有走得太远。我假设Mongo服务器使用docker容器(名为sut)运行的服务器,例如我可以通过
直接从Python处理MongoClient(host="localhost", port=27017)
不幸的是,我遇到了解决上述docker-compose的问题,例如:
thomas@linuxclientlobnek01:~/github/pydata$ docker-compose -f docker-compose.test.yml up
Starting pydata_mongo_1
Recreating pydata_sut_1
Attaching to pydata_mongo_1, pydata_sut_1
mongo_1 | 2016-07-18T10:17:03.967+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=cdaf9f862a16
mongo_1 | 2016-07-18T10:17:03.967+0000 I CONTROL [initandlisten] db version v3.2.8
mongo_1 | 2016-07-18T10:17:03.967+0000 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
mongo_1 | 2016-07-18T10:17:03.967+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013
mongo_1 | 2016-07-18T10:17:03.967+0000 I CONTROL [initandlisten] allocator: tcmalloc
mongo_1 | 2016-07-18T10:17:03.967+0000 I CONTROL [initandlisten] modules: none
mongo_1 | 2016-07-18T10:17:03.967+0000 I CONTROL [initandlisten] build environment:
mongo_1 | 2016-07-18T10:17:03.967+0000 I CONTROL [initandlisten] distmod: debian71
mongo_1 | 2016-07-18T10:17:03.967+0000 I CONTROL [initandlisten] distarch: x86_64
mongo_1 | 2016-07-18T10:17:03.967+0000 I CONTROL [initandlisten] target_arch: x86_64
mongo_1 | 2016-07-18T10:17:03.967+0000 I CONTROL [initandlisten] options: {}
mongo_1 | 2016-07-18T10:17:03.976+0000 I - [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongo_1 | 2016-07-18T10:17:03.976+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=8G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
mongo_1 | 2016-07-18T10:17:04.076+0000 I CONTROL [initandlisten]
mongo_1 | 2016-07-18T10:17:04.076+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
mongo_1 | 2016-07-18T10:17:04.076+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
mongo_1 | 2016-07-18T10:17:04.076+0000 I CONTROL [initandlisten]
mongo_1 | 2016-07-18T10:17:04.076+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
mongo_1 | 2016-07-18T10:17:04.076+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
mongo_1 | 2016-07-18T10:17:04.076+0000 I CONTROL [initandlisten]
mongo_1 | 2016-07-18T10:17:04.076+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongo_1 | 2016-07-18T10:17:04.076+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
mongo_1 | 2016-07-18T10:17:04.077+0000 I NETWORK [initandlisten] waiting for connections on port 27017
pydata_sut_1 exited with code 0
非常感谢任何评论...
答案 0 :(得分:2)
端口27017
未发布到您的主机,因此您无法从localhost连接到它。
您可以将mongo
阻止更改为
mongo:
image: mongo
ports:
- 27017:27017
或通过docker容器名称从docker容器连接:
MongoClient(host="mongo", port=27017)
答案 1 :(得分:0)
一段时间前,我遇到了同样的问题,还有更多,所以我写了一篇简短的文章/指南:MongoDB in Docker-compose - installation in few steps (2019)
很抱歉在我自己的博客上发布无耻的广告,但我希望它可以节省某人的时间...