I am working on a test docker-compose file to run a container with three mongod servers, which form a replica set (One is primary, one is secondary and one is arbiter).
My Dockerfile looks like :-
FROM mongo:3.6.2
MAINTAINER Shanty
RUN apt-get update && apt-get install -y netcat
COPY ./.docker/mongo_scripts /mongo_scripts
RUN chmod +rx /mongo_scripts/*.sh
EXPOSE 27091 27092 27093
ENTRYPOINT ["/mongo_scripts/rs.sh"]
and my script looks like :-
#!/bin/bash
# shell script to create a simple mongodb replica set (tested on osx)
mkdir -p /mongosvr/rs-0
mkdir -p /mongosvr/rs-1
mkdir -p /mongosvr/rs-2
mongod --dbpath /mongosvr/rs-0 --bind_ip_all --replSet "demo" --port 27091 --pidfilepath /mongosvr/rs-0.pid 2>&1 &
mongod --dbpath /mongosvr/rs-1 --bind_ip_all --replSet "demo" --port 27092 --pidfilepath /mongosvr/rs-1.pid 2>&1 &
mongod --dbpath /mongosvr/rs-2 --bind_ip_all --replSet "demo" --port 27093 --pidfilepath /mongosvr/rs-2.pid 2>&1 &
# wait a bit for the first server to come up
sleep 5
# call rs.initiate({...})
cfg="{
_id: 'demo',
members: [
{_id: 0, host: 'localhost:27091', 'priority':10},
{_id: 1, host: 'localhost:27092'},
{_id: 2, host: 'localhost:27093', arbiterOnly: true}
]
}"
mongo localhost:27091 <<EOF
var cfg={
_id: 'demo',
members: [
{_id: 0, host: 'localhost:27091', 'priority':10},
{_id: 1, host: 'localhost:27092'},
{_id: 2, host: 'localhost:27093', arbiterOnly: true}
]
}
rs.initiate(cfg, { force: true });
EOF
Its a simple script running three mongod servers, and initialising replica set configuration. But after the last command is successfully executed, the following is logged :-
bye
I NETWORK [conn1] end connection 127.0.0.1:55178 (2 connections now open)
and my container stops:-
mongodb_rs exited with code 0
I guess its some issue with setting replica set config, after which some termination command is send....
Only thing that worked as of now is that I put following command at the end of the script:-
sleep infinity
I still don't know whether its the best way.... Still looking for better solution, or explanation if the current one is the best....