我想使用docker-compose
创建3个节点的副本集,并为它们提供初始数据。如果我删除--replSet
并播种数据而未指定主机我没有问题。
搬运工-compose.yml
master:
image: 'mongo:3.4'
ports:
- '50000:27017'
volumes:
- './restaurants.json:/restaurants.json'
- './setup.js:/docker-entrypoint-initdb.d/00_setup.js'
- './seed.sh:/docker-entrypoint-initdb.d/01_seed.sh'
command: '--replSet rs'
slave1:
image: 'mongo:3.4'
ports:
- '50001:27017'
command: '--replSet rs'
slave2:
image: 'mongo:3.4'
ports:
- '50002:27017'
command: '--replSet rs'
seed.sh
# ...
_wait "slave1"
_wait "slave2"
echo "Starting to import data..."
mongoimport --host="rs/master:27017,slave1:27017,slave2:27017" --db db --collection restaurants --drop --file /restaurants.json > /dev/null
echo "Done."
日志
master_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/01_seed.sh
master_1 | Waiting for slave1...
master_1 | .
master_1 | Done.
master_1 | Waiting for slave2...
master_1 | Done.
master_1 | Starting to import data...
master_1 | 2017-11-26T16:06:39.148+0000 [........................] db.restaurants 0B/11.3MB (0.0%)
master_1 | 2017-11-26T16:06:39.653+0000 [........................] db.restaurants 0B/11.3MB (0.0%)
master_1 | 2017-11-26T16:06:39.653+0000 Failed: error connecting to db server: no reachable servers
master_1 | 2017-11-26T16:06:39.653+0000 imported 0 documents
mongoreplication_master_1 exited with code 1
答案 0 :(得分:0)
这个问题很老但我最近遇到了同样的问题,值得注意的是mongo docker-entrypoint.sh
脚本会在initDb阶段删除--replicaSet
参数,请参阅:
https://github.com/docker-library/mongo/blob/master/3.6/docker-entrypoint.sh#L237
因此,您无法连接到运行init脚本的主机,您可以创建另一个容器,其唯一目的是初始化复制集,然后覆盖docker-entrypoint.sh