I am setting up a docker container to initialize the Mongo DB replica set using docker compose. I am not able to get this done. I have created docker-compose to invoke three mongo instance but i am getting parser issue. Below is my Docker-Compose file.
version: '3'
services:
mongo-vauto-1:
image: "mongo-start"
build: ./mongo-vAUTO-1
ports:
- "30000:27017"
volumes:
- ./mongo-vAUTO-1/data:/data/db
depends_on:
- "mongo-vauto-2"
- "mongo-vauto-3"
mongo-vauto-2:
image: "mongo"
command: --replSet vAUTO --smallfiles --oplogSize 128
ports:
- "40000:27017"
volumes:
- ./mongo-vAUTO-2/data:/data/db
mongo-vauto-3:
image: "mongo"
command: --replSet vAUTO --smallfiles --oplogSize 128
ports:
- "50000:27017"
volumes:
- ./mongo-vAUTO-3/data:/data/db
mongo-setup-vauto:
image: "mongo-setup-vauto"
build: ./setup
depends_on:
- "mongo-vauto-1"
which invokes Dockerfile which is in ./setup folder. Below is my set up Dockerfile
FROM mongo
# Create app directory
WORKDIR vauto-docker/src/config
# Install app dependencies
COPY replicaSet.js .
COPY setup.sh .
CMD ["./setup.sh"]
which invokes replicaSet.js and setup.sh. The replicaSet.js and setup.sh files are provided below
# replicaSet.js
rsconf = {
_id : "vAUTO",
members: [
{_id : 0, host : "mongo-vauto-1:27017"},
{_id : 1, host : "mongo-vauto-2:27017"},
{_id : 2, host : "mongo-vauto-3:27017"}
]
}
rs.initiate(rsconf);
rs.conf();
# setup.sh
echo *********************************
echo Starting the replica set
echo *********************************
sleep 10 | echo waiting for the replica set to be intialized
mongo mongodb://mongo-vauto-1:27017 replicaSet.js
Dockerfile for first replica set is given below
FROM mongo
WORKDIR vauto-docker/src/config
copy mongo.conf .
EXPOSE 27017
CMD ["--config","./mongo.conf"]
and my mongo.conf file has the following code
replication:
oplogSizeMB: 1024
replSetName: vAUTO
I am getting the following error,
mongo-vauto-1_1 | parse error: Invalid numeric literal at line 1, column 14
mongo-vauto-1_1 | parse error: Invalid numeric literal at line 1, column 14
mongo-vauto-1_1 | Error parsing YAML config file: yaml-cpp: error at line 3, column 1: illegal tab when looking for indentation
Here is how I did this using a 4th container to initialize the replica set after 3 mongodb containers are running:
Docker-compose file
version: '3'
services:
mongodb1:
image: mongo:latest
networks:
- alphanetwork
volumes:
- data1:/data/db
- config1:/data/configdb
ports:
- 30001:27017
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongodb2:
image: mongo:latest
networks:
- alphanetwork
ports:
- 30002:27017
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongodb3:
image: mongo:latest
networks:
- alphanetwork
ports:
- 30003:27017
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongoclient:
image: mongo
networks:
- alphanetwork
depends_on:
- mongodb1
- mongodb2
- mongodb3
volumes:
- ./deployment_scripts:/deployment_scripts
entrypoint:
- /deployment_scripts/initiate_replica.sh
networks:
alphanetwork:
volumes:
data1:
config1:
initiate_replica.sh
#!/bin/bash
echo "Starting replica set initialize"
until mongo --host mongodb1 --eval "print(\"waited for connection\")"
do
sleep 2
done
echo "Connection finished"
echo "Creating replica set"
mongo --host mongodb1 <<EOF
rs.initiate(
{
_id : 'rs0',
members: [
{ _id : 0, host : "mongodb1:27017" },
{ _id : 1, host : "mongodb2:27017" },
{ _id : 2, host : "mongodb3:27017" }
]
}
)
EOF
echo "replica set created"
And don't forget to chmod +x ./deployment_scripts/initiate_replica.sh
to give docker the permission to execute it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With