0

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

Shubham Gupta
  • 159
  • 1
  • 14

4 Answers4

0

you should not override the ENTRYPOINT in your Dockerfile cause it might do more stuff for setting up the container from mongo team. Try to replace the ENTRYPOINT with CMD

CMD ["/mongo_scripts/rs.sh"] 
Mazel Tov
  • 2,064
  • 14
  • 26
  • Tried. Didn't work. However, it works with even entry point when I don't add the last command of setting replica set configuration.. – Shubham Gupta Mar 18 '18 at 08:08
0

Add

sh -c /bin/bash

at the end of your bash script. It will start a new process bash with a new pid. So your container will not die.

MB11
  • 610
  • 1
  • 5
  • 16
0

When your script starts a mongod process, it only exists within the scope of the script; so when the script ends, it shuts down each mongod process.

To make them persistent, you need to add the --fork option :

Enables a daemon mode that runs the mongod process in the background. By default mongod does not run as a daemon: typically you will run mongod as a daemon, either by using --fork or by using a controlling process that handles the daemonization process (e.g. as with upstart and systemd).

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
0

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

Shubham Gupta
  • 159
  • 1
  • 14