mongod --bind_ip 使用 docker-compose 版本 2 [英] mongod --bind_ip using docker-compose version 2

查看:56
本文介绍了mongod --bind_ip 使用 docker-compose 版本 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 docker-compose 连接在 docker 上运行的两个 mongodb 容器.一个容器使用 mongoimport 将一些数据种子给另一个容器.

但是,运行 mongoimport

时出现以下错误

失败:连接到数据库服务器时出错:没有可访问的服务器

我实际上可以从 mongo-seed 访问 mongodb 容器,因为我已将 ping -c 3 mongodb 附加到 >CMD 并且容器可以成功解析该主机名.

在我使用的文件下方:

docker-compose.yml

版本:'2'服务:mongodb:图像:mongo:3.2端口:- 27017:27017"mongo 种子:构建:./mongo-seed

mongo-seed/Dockerfile

来自 mongo:3.2复制 init.json/init.jsonCMD ping -c 3 mongodb &&mongoimport --host mongodb --db test --collection users --type json --file/init.json --jsonArray

mongo-seed/init.json

<预><代码>[{"name": "乔·史密斯","email": "jsmith@gmail.com",年龄":40,管理员":假},{"name": "Jen Ford","email": "jford@gmail.com",年龄":45,管理员":真}]

这是docker-compose up的输出:

mongo-seed_1 |PING mongodb (172.18.0.2):48 个数据字节mongo-seed_1 |来自 172.18.0.2 的 56 个字节:icmp_seq=0 ttl=64 time=0.116 msmongo-seed_1 |来自 172.18.0.2 的 56 个字节:icmp_seq=1 ttl=64 time=0.141 msmongo-seed_1 |来自 172.18.0.2 的 56 个字节:icmp_seq=2 ttl=64 time=0.114 msmongo-seed_1 |--- mongodb ping 统计 ---mongo-seed_1 |发送 3 个数据包,接收 3 个数据包,0% 丢包率mongo-seed_1 |往返 min/avg/max/stddev = 0.114/0.124/0.141/0.000 msmongo-seed_1 |2016-08-09T20:34:15.728+0000 [........................] smtt.devices 0.0 B/25.5 MB (0.0%)mongo-seed_1 |2016-08-09T20:34:17.992+0000 [........................] smtt.devices 0.0 B/25.5 MB (0.0%)mongo-seed_1 |2016-08-09T20:34:17.992+0000 失败:连接到数据库服务器时出错:没有可访问的服务器mongo-seed_1 |2016-08-09T20:34:17.992+0000 导入 0 个文件mongo_mongo-seed_1 退出,代码为 1

有什么想法吗?我错过了什么?

解决方案

我终于可以连接两个容器了.我的发现用于文档目的.

要点

  • docker-compose 版本 2 创建一个 bridge 网络并将所有容器添加到其中.
  • mongod 不知道这个网络,因此不会绑定到那个 ip.默认情况下,只绑定到 127.0.0.1
  • mongoimport 无法使用容器名称建立连接,即使它应该被转换为容器 ip.

解决方案

  • 通过network
  • 的显式定义为mongodb分配一个静态IP
  • 初始化 mongo 容器,--bind_ip 标志指向该静态 ip
  • mongoimport
  • 中使用 IP 地址而不是主机名

docker-compose.yml

版本:'2'服务:mongodb:图像:mongo:3.2端口:- 27017:27017"网络:蒙戈网:ipv4_address: 172.16.0.1命令:mongod --bind_ip 127.0.0.1,172.16.0.1mongo 种子:构建:./mongo-seed网络:蒙戈网:依赖于取决于:- MongoDB网络:蒙戈网:司机:桥ipam:配置:- 子网:172.16.0.0/24网关:172.16.0.254

mongo-seed/Dockerfile

来自 mongo:3.2复制 init.json/init.jsonCMD mongoimport --host 172.16.0.1 --db test --collection users --type json --file/init.json --jsonArray

I'm trying to connect two mongodb containers running on docker using docker-compose. One container seed some data to the other using mongoimport.

However, I'm getting the following error when run mongoimport

Failed: error connecting to db server: no reachable servers

I can actually reach the mongodb container from mongo-seed since I'd appended a ping -c 3 mongodb to the CMD and the container can resolve successfully that hostname.

Below the files I'm using:

docker-compose.yml

version: '2'

services:
  mongodb:
    image: mongo:3.2
    ports:
      - "27017:27017"

  mongo-seed:
    build: ./mongo-seed

mongo-seed/Dockerfile

FROM mongo:3.2

COPY init.json /init.json
CMD ping -c 3 mongodb && mongoimport --host mongodb --db test --collection users 
   --type json --file /init.json --jsonArray

mongo-seed/init.json

[
  {
    "name": "Joe Smith",
    "email": "jsmith@gmail.com",
    "age": 40,
    "admin": false
  },
  {
    "name": "Jen Ford",
    "email": "jford@gmail.com",
    "age": 45,
    "admin": true
  }
]

And this the output of docker-compose up:

mongo-seed_1  | PING mongodb (172.18.0.2): 48 data bytes
mongo-seed_1  | 56 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.116 ms
mongo-seed_1  | 56 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.141 ms
mongo-seed_1  | 56 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.114 ms
mongo-seed_1  | --- mongodb ping statistics ---
mongo-seed_1  | 3 packets transmitted, 3 packets received, 0% packet loss
mongo-seed_1  | round-trip min/avg/max/stddev = 0.114/0.124/0.141/0.000 ms
mongo-seed_1  | 2016-08-09T20:34:15.728+0000    [........................] smtt.devices 0.0 B/25.5 MB (0.0%)
mongo-seed_1  | 2016-08-09T20:34:17.992+0000    [........................] smtt.devices 0.0 B/25.5 MB (0.0%)
mongo-seed_1  | 2016-08-09T20:34:17.992+0000    Failed: error connecting to db server: no reachable servers
mongo-seed_1  | 2016-08-09T20:34:17.992+0000    imported 0 documents
mongo_mongo-seed_1 exited with code 1

Any idea? What am I missing?

解决方案

I could finally manage to get connected both containers. My findings here for documentation purposes.

Main points

  • docker-compose version 2 creates a bridge network and adds all the containers to it.
  • mongod is not aware of this network and therefore doesn't bind to that ip. By default, binds only to 127.0.0.1
  • mongoimport could not establish connection using container name, even though it's supposed to be translated to the container ip.

Solution

  • Assign an static IP for the mongodb through the explicit definition of a network
  • Init mongo container with --bind_ip flag pointing to that static ip
  • Use ip address instead of hostname with mongoimport

docker-compose.yml

version: '2'

services:
  mongodb:
    image: mongo:3.2
    ports:
      - "27017:27017"
    networks:
      mongo_net:
        ipv4_address: 172.16.0.1
    command: mongod --bind_ip 127.0.0.1,172.16.0.1

  mongo-seed:
    build: ./mongo-seed
    networks:
      mongo_net:
    depends_on:
      - mongodb

networks:
  mongo_net:
    driver: bridge
    ipam:
      config:
      - subnet: 172.16.0.0/24
        gateway: 172.16.0.254

mongo-seed/Dockerfile

FROM mongo:3.2

COPY init.json /init.json
CMD mongoimport --host 172.16.0.1 --db test --collection users 
   --type json --file /init.json --jsonArray

这篇关于mongod --bind_ip 使用 docker-compose 版本 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆