This is the script I managed to create :


echo "Updating the system, this might take a couple minutes..."
sudo yum -y update

echo "Installing Docker"
curl -fsSL https://get.docker.com/ | sh

echo "Starting Services"
sudo systemctl start docker
sudo systemctl enable docker

echo "Creating Network..."
sudo docker network create mongo-cluster

echo "Creating 3 MongoDB Instances"
sudo docker run -p 30001:27017 --name primary --net mongo-cluster mongo 
mongod --replSet comments && sudo docker start primary

sudo docker run -p 30002:27017 --name failover1 --net mongo-cluster mongo 
mongod --replSet comments && sudo docker start failover1

sudo docker run -p 30003:27017 --name failover2 --net mongo-cluster mongo 
mongod --replSet comments && sudo docker start failover2

docker exec -it primary bash -c "echo 'primary = (new 
Mongo(\"localhost:27017\")).getDB(\"test\")' | mongo && echo 'config = 
{\"_id\": \"comments\",\"members\": [{\"_id\": 0,\"host\": 
\"primary:27017\"},{\"_id\": 1,\"host\":\"failover1:27017\"},{\"_id\": 
2,\"host\": \"failover2:27017\"}]}' | mongo && echo 'rs.initiate(config)' | 

For some reason, when the last docker command runs, it fails. I can see that the first 2 parts run well and i see the output in the command line, but the rs.initiate fails. Reason is it doesn't find any config... As far as I can tell, I need to run the 3 commands in the same mongo session otherwise it doesn't stick. For some reason when executing the last piece of script, the mongo session ends after each command. Any ideas why and how to fix it?

closed as too broad by Ipor Sircer, slm Jul 29 '18 at 22:25

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please add the output from docker logs for this failed container. – slm Jul 29 '18 at 22:25
  • I don’t know mongo, but it looks like you are running three mongo sessions. Try "(echo 'primary = (new Mongo(\"localhost:27017\")).getDB(\"test\")' && echo 'config = {\"_id\": \"comments\",\"members\": [{\"_id\": 0,\"host\": \"primary:27017\"},{\"_id\": 1,\"host\":\"failover1:27017\"},{\"_id\": 2,\"host\": \"failover2:27017\"}]}' && echo 'rs.initiate(config)') | mongo"; … … … … … … … … … … … … … … … … …  i.e.,  "(echo 'primary = …' && echo 'config = …' && echo 'rs.initiate…') | mongo". – G-Man Jul 29 '18 at 22:52
  • @G-Man you're a genius! It works! Thanks a lot I really appreciate it :) – BrainSweetiesss Jul 29 '18 at 23:59