[英]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: 您应该记住:
I strongly recommend that you make three changes: 我强烈建议您进行三处更改:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.