无法在docker中连接到MongoDB

前端之家收集整理的这篇文章主要介绍了无法在docker中连接到MongoDB前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用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谷歌或其他东西,命令只是挂起.

我似乎只能通过在docker run命令中使用–net = host来获得从容器到外部世界的Internet连接.

最佳答案
这里的问题是 – 如果容器侦听127.0.0.1,则主机无法将端口映射到它.

你需要监听0.0.0.0或类似的,然后-p可以到达容器的界面.

原文链接:https://www.f2er.com/docker/437093.html

猜你在找的Docker相关文章