无法使用docker-compose运行具有副本集的mongo容器 [英] Unable to run mongo container with replica set using docker-compose
本文介绍了无法使用docker-compose运行具有副本集的mongo容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的docker-compose文件:
Here's my docker-compose file:
version: '3'
services:
mongo:
hostname: mongo
container_name: search_mongo
image: mongo:latest
volumes:
- ./docker/local/persist/mongo:/data/db
- ./docker/mongo:/opt/mongo
ports:
- "8884:27017"
- "8885:27018"
entrypoint: /opt/mongo/entrypoint_mongo.sh
agent:
build: .
image: myapp_search:compose
depends_on:
- mongo
这是我的entrypoint_mongo.sh
Here's my entrypoint_mongo.sh
#!/bin/bash
mongod --port 27018 --replSet rs0 --fork --syslog --smallfiles
mongo --port 27018 --eval "rs.initiate({_id : 'rs0', members : [{_id : 0, host : 'mongo:27018'}]})"
mongo --port 27018 --eval "while(true) {if (rs.status().ok) break;sleep(1000)};"
我面临的问题是: mongo容器正在成功执行其所有步骤,但以状态0退出.
The issue I am facing is : The mongo container is executing all its steps successfully but its exiting with status 0.
mongo_1 | about to fork child process, waiting until server is ready for connections.
mongo_1 | forked process: 7
mongo_1 | child process started successfully, parent exiting
mongo_1 | MongoDB shell version v3.4.10
mongo_1 | connecting to: mongodb://127.0.0.1:27018/
mongo_1 | MongoDB server version: 3.4.10
mongo_1 | { "ok" : 1 }
mongo_1 | MongoDB shell version v3.4.10
mongo_1 | connecting to: mongodb://127.0.0.1:27018/
mongo_1 | MongoDB server version: 3.4.10
search_mongo exited with code 0
推荐答案
您的启动脚本不应初始化或监视副本集;这些应该是手动任务.
Your startup script should not initialise or monitor the replicaset; those should be manual tasks.
您应该记住:
- 启动副本集严格来说是一次性的工作;一旦启动,MongoDB服务在重新启动后将继续成为同一副本集的一部分. 副本集通常包含几个可以互换的节点;如果它们中的每一个都尝试在启动时初始化副本集,则会抛出错误
- 重新启动服务是正常的,预期的行为;例如,当您升级到下一个版本的MongoDB时,或者在重新启动服务器主机的修补程序之后,或者在断电之后
- 如果您的脚本在每次启动MongoDB服务时尝试初始化已初始化的副本集,则会引发错误
- initiating a replica set is strictly a one-off job; once it is initiated, the MongoDB service, when restarted, will continue being part of the same replica set.
- a replica set normally contains several nodes which should be interchangable; if each of them tries to initialise the replica set on startup, they will throw errors
- restarting a service is normal, expected behaviour; for example when you upgrade to the next version of MongoDB, or after patches to your server host require a reboot, or after a power outage
- if your script tries to initialise an already-initialised replica set each time it starts the MongoDB service, it will throw errors
我强烈建议您进行三处更改:
I strongly recommend that you make three changes:
- 让您的mongo容器只运行mongo,而无需执行初始化和监视副本集的步骤.
- 如果要运行副本集,请以受控的手动方式小心启动它;同上,如果要添加/删除节点或重新配置.
- 如果要监视副本集的运行状况,请使用单独的工具执行此操作;否则,请执行以下操作.让mongo服务完成其常规工作.
这篇关于无法使用docker-compose运行具有副本集的mongo容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文