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
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)};"
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
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.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: