Reputation: 2573
I have a node back-end application which requires MongoDB with replica set. I have created a docker image for my application which simply runs the application; also created a docker-compose
file which runs the MongoDB instance and config its replica set.
here is my docker-compose
file:
version: '2'
services:
mongod1:
image: mongo
command: mongod --replSet ${RS} --oplogSize 16 --smallfiles --noprealloc --bind_ip 0.0.0.0
ports:
- '27017:27017'
mongod2:
image: mongo
command: mongod --replSet ${RS} --oplogSize 16 --smallfiles --noprealloc --bind_ip 0.0.0.0
ports:
- '27018:27017'
mongod3:
image: mongo
command: mongod --replSet ${RS} --oplogSize 16 --smallfiles --noprealloc --bind_ip 0.0.0.0
ports:
- '27019:27017'
mongo-config:
image: mongo
depends_on:
- mongod1
- mongod2
- mongod3
volumes:
- ./scripts:/scripts
environment:
- MONGO1=mongod1
- MONGO2=mongod2
- MONGO3=mongod3
- USER=machine
- PWD=abc123456
- RS=rs0
entrypoint: ['/scripts/setup.sh']
and in the entrypoint I have the bash script like below:
#!/bin/bash
mongodb1=$(getent hosts ${MONGO1} | awk '{ print $1 }')
mongodb2=$(getent hosts ${MONGO2} | awk '{ print $1 }')
mongodb3=$(getent hosts ${MONGO3} | awk '{ print $1 }')
port=${PORT:-27017}
echo "Waiting for startup.."
until mongo --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do
printf '.'
sleep 1
done
echo "Started.."
echo "setup.sh; time now: $(date +"%T")"
mongo --host ${mongodb1}:${port} <<EOF
use admin
db.createUser(
{
user: ${USER},
pwd: #{pwd},
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
EOF
mongo --host ${mongodb1}:${port} <<EOF
var cfg = {
"_id": "${RS}",
"members": [
{
"_id": 0,
"host": "${mongodb1}:${port}"
},
{
"_id": 1,
"host": "${mongodb2}:${port}"
},
{
"_id": 2,
"host": "${mongodb3}:${port}"
}
]
};
rs.initiate(cfg, { force: true });
rs.config();
EOF
mongo --host ${mongodb2}:${port} <<EOF
db.setSlaveOk()
EOF
mongo --host ${mongodb3}:${port} <<EOF
db.setSlaveOk()
EOF
now im my envirement i can connect to mongodb in localhost:27017
but application cannot connect to 0.0.0.0:27017
.
how I can fix this? should i use docker networks?
Upvotes: 2
Views: 2380
Reputation: 78
try to change 0.0.0.0 to 127.0.0.1 or just use localhost.
BTW, I cannot create auth from your method by keeping getting Error: couldn't add user: not master
, so I searched around, and found out that before rs.initiate() has been called, and if a mongod.conf
with a setting of replication.replSetName: rs
existed, the mongo instance you are connecting to will have no idea who is master or Primary.
so I switched the order of createUser
and rs.initiate()
, and put a delay, then it works fine.
Thanks for the skeleton.
version: "3.3"
services:
mongo-admin:
image: mongo-express
container_name: "mongo-admin"
environment:
- ME_CONFIG_MONGODB_SERVER=db_manager,db_worker_1,db_worker_2
- ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
- ME_CONFIG_BASICAUTH_USERNAME=$MONGOADMIN
- ME_CONFIG_BASICAUTH_PASSWORD=$MONGOPASS
ports:
- 8081:8081
restart: on-failure
depends_on:
- db_manager
- db_worker_1
- db_worker_2
- mongo-set-setup
db_manager:
image: mongo:4.0
container_name: db_manager
volumes:
- mongodb1:/data/db
- ./mongod.conf:/etc/mongod.conf
command: mongod --config /etc/mongod.conf
ports:
- "27017:27017"
restart: always
db_worker_1:
image: mongo:4.0
container_name: db_worker_1
volumes:
- mongodb2:/data/db
- ./mongod.conf:/etc/mongod.conf
command: mongod --config /etc/mongod.conf
ports:
- "27018:27017"
restart: always
db_worker_2:
image: mongo:4.0
container_name: db_worker_2
volumes:
- mongodb3:/data/db
- ./mongod.conf:/etc/mongod.conf
command: mongod --config /etc/mongod.conf
ports:
- "27019:27017"
restart: always
mongo-set-setup:
container_name: "mongo-set-setup"
image: mongo:4.0
depends_on:
- db_manager
- db_worker_1
- db_worker_2
volumes:
- ./setupmongo.sh:/scripts/setupmongo.sh
environment:
- MONGO1=db_manager
- MONGO2=db_worker_1
- MONGO3=db_worker_2
- USER=$MONGOUSER
- PASSWORD=$MONGOPASSWORD
- RS=rs
entrypoint: ["/scripts/setupmongo.sh"]
#
volumes:
mongodb1:
mongodb2:
mongodb3:
and shell
#!/bin/bash
mongodb1=`getent hosts ${MONGO1} | awk '{ print $1 }'`
mongodb2=`getent hosts ${MONGO2} | awk '{ print $1 }'`
mongodb3=`getent hosts ${MONGO3} | awk '{ print $1 }'`
port=${PORT:-27017}
echo "Waiting for startup.."
until mongo --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do
printf '.'
sleep 1
done
echo "Started.."
echo ~~~~~~~~setup replic time now: `date +"%T" `
echo ${mongodb1}, ${mongodb2}, ${mongodb3}
mongo --host ${mongodb1}:${port} <<EOF
var cfg = {
"_id": "${RS}",
"protocolVersion": 1,
"members": [
{
"_id": 0,
"host": "${mongodb1}:${port}",
"priority": 2
},
{
"_id": 1,
"host": "${mongodb2}:${port}",
"priority": 0
},
{
"_id": 2,
"host": "${mongodb3}:${port}",
"priority": 0
}
]
};
rs.initiate(cfg, { force: true });
rs.reconfig(cfg, { force: true });
rs.slaveOk();
db.getMongo().setReadPref('nearest');
db.getMongo().setSlaveOk();
EOF
sleep 30
echo ${USER}, ${PASSWORD}
echo ~~~~~~~~~~setup user auth time now: `date +"%T" `
mongo --host ${mongodb1}:${port} <<EOF
use admin
db.createUser(
{
"user": "${USER}",
"pwd": "${PASSWORD}",
"roles": [ "readWrite"]
}
)
EOF
Upvotes: 3