I am a Docker novice and have been trying for a long time, but I still cannot create the containers as expected.
This is my current configuration, but it does not meet the expectations: MongoServerError: no replset config has been received
version: "3.8"
services:
RoteMongoFirst:
image: mongo:5
command: mongod --replSet roteReplicaSet --bind_ip localhost,RoteMongoFirst
container_name: RoteMongoFirst
ports:
- "27017:27017"
environment:
- MONGO_INITDB_DATABASE=Rote
- MONGO_REPLICA_SET_MODE=primary
volumes:
- ./mongodb/RoteMongoFirst:/data/db
- ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
networks:
- rote-network
RoteMongoSecond:
image: mongo:5
depends_on:
- RoteMongoFirst
command: >
bash -c "mongod --replSet roteReplicaSet --bind_ip localhost,RoteMongoSecond &&
mongosh --eval "rs.initiate({ _id: \"roteReplicaSet\", members: [ { _id: 0, host: \"RoteMongoFirst\" }, { _id: 1, host: \"RoteMongoSecond\" } ]})""
container_name: RoteMongoSecond
ports:
- "27018:27017"
environment:
- MONGO_INITDB_DATABASE=Rote
- MONGO_REPLICA_SET_MODE=secondary
volumes:
- ./mongodb/RoteMongoSecond:/data/db
- ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
networks:
- rote-network
networks:
rote-network:
2
Answers
After spending a long time, I finally wrote a docker-compose file that meets expectations. It implements the creation of a MongoDB cluster and initializes a database (Rote) containing a collection (Rote). I hope it can help some people. 😎
Using multiple entries in
--bind_ip
often causes trouble. I would suggest to use either--bind_ip localhost
or--bind_ip_all
. Using--bind_ip
with an IP makes only sense if your computer has multiple network interfaces. However, MongoDB does not support multiple network interfaces, so still it does not make much sense.Regarding you actual problem: You need to wait a bit till ReplicaSet is initiated. Try to add
after your
rs.initiate(..)
command.