Hi everyne,
This is how my docker-compose.yml looks like:
version: '3.7'
services:
mongo-1:
image: mongo:4.4.2
container_name: mongo-1
ports:
- 27030:27017
networks:
- mongo
restart: always
command: /usr/bin/mongod --bind_ip_all --replSet rs0 --journal --dbpath /data/db --enableMajorityReadConcern false
volumes:
- ./mongo-1/db:/data/db
mongo-2:
image: mongo:4.4.2
container_name: mongo-2
ports:
- 27031:27017
networks:
- mongo
restart: always
command: /usr/bin/mongod --bind_ip_all --replSet rs0 --journal --dbpath /data/db --enableMajorityReadConcern false
volumes:
- ./mongo-2/db:/data/db
mongo-3:
image: mongo:4.4.2
container_name: mongo-3
ports:
- 27032:27017
networks:
- mongo
restart: always
command: /usr/bin/mongod --bind_ip_all --replSet rs0 --journal --dbpath /data/db --enableMajorityReadConcern false
volumes:
- ./mongo-3/db:/data/db
volumes:
mongo-1:
mongo-2:
mongo-3:
networks:
mongo:
Iâll create volumes in production instead biding, but not important right now.
After I execute docker container ls
I can see the following:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4de461506c94 mongo:4.4.2 "docker-entrypoint.sâŠ" 18 seconds ago Up 16 seconds 0.0.0.0:27031->27017/tcp mongo-2
2f6ff3f6cbef mongo:4.4.2 "docker-entrypoint.sâŠ" 18 seconds ago Up 16 seconds 0.0.0.0:27032->27017/tcp mongo-3
8a467ad33809 mongo:4.4.2 "docker-entrypoint.sâŠ" 18 seconds ago Up 16 seconds 0.0.0.0:27030->27017/tcp mongo-1
and then I do the next:
docker exec -it mongo-1 mongo
which gets me into the mongo shell.
If I execte rs.stats()
it will get me the following:
rs.status();
{
âoperationTimeâ : Timestamp(0, 0),
âokâ : 0,
âerrmsgâ : âno replset config has been receivedâ,
âcodeâ : 94,
âcodeNameâ : âNotYetInitializedâ,
â$clusterTimeâ : {
âclusterTimeâ : Timestamp(0, 0),
âsignatureâ : {
âhashâ : BinData(0,âAAAAAAAAAAAAAAAAAAAAAAAAAAA=â),
âkeyIdâ : NumberLong(0)
}
}
}
Now I want to add replica members and Iâll execute the next:
rs.initiate({
_id : 'rs0',
members: [
{ _id : 0, host : "mongo-1:27017" },
{ _id : 1, host : "mongo-2:27017" },
{ _id : 2, host : "mongo-3:27017" }
]
});
And this is where problems begin.
I cannot login to replica set, but can on individual nodes. For example, I create a new document in primary replica and without any problems I can see that docment in secondary nodes (when I log in individually).
I tried this on my Mac OS X, but the same thing is on Linux too.
This is my error message:
Cannot connect to replica set "localhost:replica"[localhost:27030].
Set's primary is unreachable.
Reason:
No member of the set is reachable. Reason: Connect failed
and this is how my connection looks:
Members:
localhost:27030
localhost:27031
localhost:27032
Set Name: rs0
Note that this is is Robo 3T. It works on Atlas, for example.
Does anyone know what should I do?
I spent last 2 days on this and I think I checked every tutorial, every YouTube video, bu still nothing.
Seems like Mongo Dockerfile is hard coded to expose it to port 27017, so itâs not possible to change that.
I also tried with:
extra_hosts:
- mongo-1:127.0.0.1
in each service but nothing.
Thanks.