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:
# SETUP MONGODB WITHOUT AUTH
https://gist.github.com/asoorm/7822cc742831639c93affd734e97ce4f
# SETUP MONGODB WITH AUTH AND X509 CERT VALIDATION (FOR PROD)
https://prashix.medium.com/setup-mongodb-replicaset-with-authentication-enabled-using-docker-compose-5edd2ad46a90
#!/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"