无法连接到工作的Dockerized Mongo ReplSet

时间:2018-09-10 22:18:26

标签: mongodb docker replicaset

我正在尝试使用以下docker-compose文件创建在本地运行的Mongo ReplicaSet:

version: '2.0'

services:
  db01:
    image: mongo:3.4
    mem_limit: 512m
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - datadb01:/data/db
      - ./etc/mongod.conf:/etc/mongod.conf
    command: mongod --smallfiles --noIndexBuildRetry --replSet rs0
    container_name: db01
    networks:
      - mongo

  db02:
    image: mongo:3.4
    mem_limit: 512m
    restart: always
    ports:
      - "27018:27017"
    volumes:
      -  datadb02:/data1/db
      - ./etc/mongod.conf:/etc/mongod.conf
    command: mongod --smallfiles --noIndexBuildRetry --replSet rs0
    container_name: db02
    networks:
      - mongo

所以这实际上似乎可行!我不会发布全部内容,但是当我运行rs.status()时,我会得到:

{
  "set" : "rs0",
  ...
  "ok" : 1
}

但是,当我尝试连接到copysetSet而不是单个节点时,会收到以下错误日志:

~/utils/infrastructure/content-repl-set [replay-content*]: mongo --host rs0/127.0.0.1:27017,127.0.0.1:27018
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017,127.0.0.1:27018/?replicaSet=rs0
2018-09-10T17:50:42.173-0400 I NETWORK  [js] Starting new replica set monitor for rs0/127.0.0.1:27017,127.0.0.1:27018
2018-09-10T17:50:42.176-0400 I NETWORK  [js] Successfully connected to 127.0.0.1:27018 (1 connections now open to 127.0.0.1:27018 with a 5 second timeout)
2018-09-10T17:50:42.177-0400 I NETWORK  [js] changing hosts to rs0/db01:27017,db02:27017 from rs0/127.0.0.1:27017,127.0.0.1:27018
2018-09-10T17:50:42.177-0400 I NETWORK  [ReplicaSetMonitor-TaskExecutor] Successfully connected to 127.0.0.1:27017 (1 connections now open to 127.0.0.1:27017 with a 5 second timeout)
2018-09-10T17:50:42.684-0400 W NETWORK  [js] Unable to reach primary for set rs0

从中我最大的猜测是mongo shell(和其他mongo驱动程序,例如pymongo)不知道我在docker容器中指定的容器名称。有没有办法将此信息公开给我的本地计算机?还是我记错了?谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

您正在使用MacOS吗?如果是这样,似乎docker中的那些内部IP地址无法访问,因为docker(Linux)桥接网络无法从macOS主机访问。

此处有更多内容:https://docs.docker.com/docker-for-mac/networking/