I have been trying all day to setup a replica set for MongoDB transactions automated by Docker Compose, but no success so far. Keep encountering the error where mongo1
(connected from mongo-setup
) can't connect to mongo2
and mongo3
containers, saying connection refused
.
The error message I got:
{
"ok" : 0,
"errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: mongo2:27018 failed with Error connecting to mongo2:27018 (172.30.0.5:27018) :: caused by :: Connection refused, mongo3:27019 failed with Error connecting to mongo3:27019 (172.30.0.4:27019) :: caused by :: Connection refused",
"code" : 74,
"codeName" : "NodeNotFound"
}
uncaught exception: ReferenceError: EOF is not defined :
@(shell):1:1
uncaught exception: SyntaxError: unexpected token: string literal :
@(shell):1:5
My Docker Compose:
mongo3:
hostname: mongo3
image: mongo:5.0.5-focal
networks:
- shopnetwork
volumes:
- ./data/mongo3:/data/db
ports:
- 27019:27019
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "shop-mongo-set" ]
mongo2:
hostname: mongo2
image: mongo:5.0.5-focal
networks:
- shopnetwork
volumes:
- ./data/mongo2:/data/db
ports:
- 27018:27018
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "shop-mongo-set" ]
mongo1:
hostname: mongo1
image: mongo:5.0.5-focal
networks:
- shopnetwork
volumes:
- ./data/mongo1:/data/db
ports:
- 27017:27017
depends_on:
- mongo2
- mongo3
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "shop-mongo-set" ]
mongo-setup:
image: mongo:5.0.5-focal
networks:
- shopnetwork
volumes:
- ./script:/script
depends_on:
- mongo1
- mongo2
- mongo3
entrypoint: ["/script/setup.sh"]
My setup.sh to initialize replica set:
#!/bin/bash
echo "Starting replica set initialize"
until mongo --host mongo1 --eval "print(\"waited for connection\")"
do
sleep 2
done
echo "Connection finished"
echo "Creating replica set"
mongo --host mongo1 <<EOF
config = {
"_id" : "shop-mongo-set",
"members" : [
{
"_id" : 0,
"host" : "mongo1:27017"
},
{
"_id" : 1,
"host" : "mongo2:27018"
},
{
"_id" : 2,
"host" : "mongo3:27019"
}
]
}
rs.initiate(config)
EOF
echo "Replica set created"
CodePudding user response:
so as tested with the OP there was a mixup with the network configuration in compose and host network.
when using multiple services in docker-compose each service is a different host in the compose network, so for example if we have multiple services using the same port that won't be an issue because each has a different hostname and IP.
when mapping ports
from docker network
to host network
using bridge
adapter usually only one service
can use each port and better to use ports >1024 for security and compatibility reasons.
when wanting to connect to different services in the same compose file
but no external connection from WAN
into the compose network
is needed the ports
section of the compose file or the -p
option in the docker run
command isn't needed
after fixing said miss-configuration there shouldn't be any problems around that topic