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

前端 未结 2 1735
野性不改
野性不改 2021-01-24 09:15

Here\'s my docker-compose file:

version: \'3\'
services:

  mongo:
    hostname: mongo
    container_name: search_mongo
    image: mongo:latest
    volumes:
             


        
相关标签:
2条回答
  • 2021-01-24 09:56

    If your last command at the script will never exit (i.e. stop running), your container keeps running. So, while (true);do sleep 1; done is one fix.

    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.

    0 讨论(0)
  • 2021-01-24 09:57

    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.
    • 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:

    1. Let your mongo container just run mongo, without the steps to initiate and monitor the replica set.
    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.
    0 讨论(0)
提交回复
热议问题