简体   繁体   English

无法使用docker-compose运行具有副本集的mongo容器

[英]Unable to run mongo container with replica set using docker-compose

Here's my docker-compose file: 这是我的docker-compose文件:

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

Here's my entrypoint_mongo.sh 这是我的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)};"

The issue I am facing is : The mongo container is executing all its steps successfully but its exiting with status 0. 我面临的问题是:mongo容器正在成功执行所有步骤,但是退出时的状态为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

If your last command at the script will never exit (ie stop running), your container keeps running. 如果您在脚本中执行的最后一个命令永不退出(即停止运行),则您的容器将继续运行。 So, while (true);do sleep 1; done 因此, while (true);do sleep 1; done while (true);do sleep 1; done is one fix. while (true);do sleep 1; done是一种解决方法。

If you want to have solution where you can close container when wanted, you can use something like 如果您想要一个可以在需要时关闭容器的解决方案,可以使用类似

while (! test -e /tmp/stop); do sleep 1; done; rm /tmp/stop

So, when you say at command line touch /tmp/stop , it will stop that loop what keeps container running. 因此,当您在命令行中说touch /tmp/stop ,它将停止使容器保持运行的循环。

Your startup script should not initialise or monitor the replicaset; 您的启动脚本不应初始化或监视副本集。 those should be manual tasks. 这些应该是手动任务。

You should bear in mind that: 您应该记住:

  • 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. 一旦启动,MongoDB服务在重新启动后将继续成为同一副本集的一部分。
  • 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 例如,当您升级到下一个版本的MongoDB时,或者在重新启动服务器主机的修补程序之后,或者在断电之后
  • if your script tries to initialise an already-initialised replica set each time it starts the MongoDB service, it will throw errors 如果您的脚本在每次启动MongoDB服务时尝试初始化已初始化的副本集,则会引发错误

I strongly recommend that you make three changes: 我强烈建议您进行三处更改:

  1. Let your mongo container just run mongo, without the steps to initiate and monitor the replica set. 让您的mongo容器仅运行mongo,而无需执行初始化和监视副本集的步骤。
  2. If you want to run a replica set, initiate it carefully and in a controlled manual way; 如果要运行副本集,请以受控的手动方式仔细启动它。 ditto if you want to add / remove nodes, or reconfigure. 同上(如果要添加/删除节点或重新配置)。
  3. If you want to monitor the health of your replica set, use a separate tool to do that; 如果要监视副本集的运行状况,请使用单独的工具执行此操作。 let the mongo service just do its ordinary job. 让mongo服务部门完成其日常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM