使用来自 docker hub 的官方mongo
镜像,我运行以下命令来运行它:
docker run --name api -p 127.0.0.1:27017:27017 -p 127.0.0.1:28017:28017 -d mongo
然后从另一个终端 shell 运行mongo
但是,我不断收到以下错误:
MongoDB shell version: 3.2.3
connecting to: test
2016-02-18T13:52:08.110-0700 I NETWORK [thread1] Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
2016-02-18T13:52:08.110-0700 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017]
2016-02-18T13:52:08.110-0700 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:27017' :
connect@src/mongo/shell/mongo.js:224:14
@(connect):1:6
exception: connect failed
我检查了容器的日志,一切似乎都正确:
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=73334e5089e1
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] db version v3.2.3
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] git version: b326ba837cf6f49d65c2f85e1b70f6f31ece7937
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] allocator: tcmalloc
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] modules: none
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] build environment:
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] distmod: debian71
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] distarch: x86_64
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] target_arch: x86_64
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] options: {}
2016-02-18T20:51:20.542+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),
2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten]
2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten]
2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten]
2016-02-18T20:51:20.616+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2016-02-18T20:51:20.616+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-02-18T20:51:20.633+0000 I NETWORK [initandlisten] waiting for connections on port 27017
docker 或 Mongo 出了什么问题?我到处寻找答案,但至今没有任何答案。docker 或 Mongo 出了问题吗?
编辑:
我做了一些调查,发现我无法从容器内部访问互联网。我通过运行连接到它docker exec -ti api /bin/bash
,然后尝试 ping google 或类似的东西,命令就挂起了。
--net=host
我似乎只能通过使用命令才能从容器到外界建立互联网连接docker run
。
答案1
您必须告诉容器使用它自己的 IP 地址而不是本地主机。
例如,假设你使用 生成了脚手架代码expressjs
,则必须写入routes/index.js
var mongodb = require('mongodb');
router.get('/thelist', function(req, res){
// Get a Mongo client to work with the Mongo server
var MongoClient = mongodb.MongoClient;
// Define where the MongoDB server is
var url = 'mongodb://172.17.0.5:27017/dbname';
// Connect to the server
MongoClient.connect(url, function (err, db) {
.........
哪儿172.17.0.5
是$CONTAINER_IP
你可以通过以下方式找到容器的 IP
$ docker inspect $CONTAINER_NAME | grep IPAddress
如果容器中没有互联网
使用 sudo 打开sudo vim /etc/NetworkManager/NetworkManager.conf
并注释掉该行dns=dnsmasq
,因此您现在将拥有:
[main]
plugins=ifupdown,keyfile,ofono
#dns=dnsmasq
[ifupdown]
managed=false
编辑:服务名称而不是 IP
您现在可以使用服务名称而不是容器的 IP 地址,例如:
// Define where the MongoDB server is
var url = 'mongodb://api:27017/dbname';
答案2
这里的陷阱是 - 如果容器监听 127.0.0.1,则主机无法将端口映射到它。
您需要监听0.0.0.0
或类似操作,然后-p
才能到达容器的接口。
答案3
我遇到了同样的问题,但找到了另一种方法来解决此问题。 您可以在运行 docker 镜像时传递网络参数,这样 docker 就会指向正确的本地主机。
docker run --network="host" ....