Mongo Replica Set docker compose
source link: https://gist.github.com/asoorm/7822cc742831639c93affd734e97ce4f
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
EitanK96 commented on Sep 8, 2019 •
getting this error when i used themongodb://localhost:27011,localhost:27012,localhost:27013/DB_NAME?replicaSet=rs0
url
getaddrinfo ENOTFOUND mongo1 mongo1:27017
Author
asoorm commented on Sep 8, 2019
@EitanWeMatch
are you accessing mongo replica set from the host machine or from another docker container?
From your host machine - mongodb://localhost:27011,localhost:27012,localhost:27013/REPLACE_WITH_YOUR_DB_NAME?replicaSet=rs0
should be fine.
But from within another container - you would need to swap out localhost
with mongo1
, mongo2
etc. and 27011-3
with 27017
@EitanWeMatch
I'm getting the same error, how did you do to solve it?
Thanks
Mclovinn commented on Mar 13, 2020 •
I resolved this problem using my local (host) IP:
rs.initiate({
_id : 'rs0',
members: [
{ _id : 0, host : "{YOUR_LOCAL_IP}:27011" },
{ _id : 1, host : "{YOUR_LOCAL_IP}:27012" },
{ _id : 2, host : "{YOUR_LOCAL_IP}:27013" }
]
})
> rs.initiate({
... _id : 'razrnodes',
... members: [
... { _id : 0, host : "{YOUR_LOCAL_IP}:27717" },
... { _id : 1, host : "{YOUR_LOCAL_IP}:27718" },
... { _id : 2, host : "{YOUR_LOCAL_IP}:27720", arbiterOnly: true }
... ]
... })
{
"operationTime" : Timestamp(0, 0),
"ok" : 0,
"errmsg" : "No host described in new configuration 1 for replica set razrnodes maps to this node",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig",
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
>
ShuaiGao commented on Apr 16, 2020 •
thank you code, i use wrong ports and waste lots of time
I resolved this problem using my local (host) IP:
rs.initiate({ _id : 'rs0', members: [ { _id : 0, host : "{YOUR_LOCAL_IP}:27011" }, { _id : 1, host : "{YOUR_LOCAL_IP}:27012" }, { _id : 2, host : "{YOUR_LOCAL_IP}:27013" } ] })
This worked for me too
Author
asoorm commented on May 12, 2020
I resolved this problem using my local (host) IP:
rs.initiate({ _id : 'rs0', members: [ { _id : 0, host : "{YOUR_LOCAL_IP}:27011" }, { _id : 1, host : "{YOUR_LOCAL_IP}:27012" }, { _id : 2, host : "{YOUR_LOCAL_IP}:27013" } ] })
This worked for me too
Im confused - Did you exec into the mongo node container in order to setup the cluster? e.g. follow this to the letter? https://gist.github.com/asoorm/7822cc742831639c93affd734e97ce4f#gistcomment-2707084
Because the docker containers for Mongo are in the same network. e.g. mongo1
should be able to communicate with mongo2 via it's hostname & port. mongo2:27017
.
If your mongo1 needs to go via the host machine (outside of docker) and come back into docker again, something clearly is not working.
| Failed to connect to mongo on startup - retrying in 1 sec MongoError: no mongos proxy available
api_1 | at Timeout. (/usr/app/node_modules/mongodb/lib/core/topologies/mongos.js:731:28)
api_1 | at listOnTimeout (internal/timers.js:549:17)
api_1 | at processTimers (internal/timers.js:492:7) {
api_1 | [Symbol(mongoErrorContextSymbol)]: {}
it gives me this error after doing docker-compose up
I resolved this problem using my local (host) IP:
rs.initiate({ _id : 'rs0', members: [ { _id : 0, host : "{YOUR_LOCAL_IP}:27011" }, { _id : 1, host : "{YOUR_LOCAL_IP}:27012" }, { _id : 2, host : "{YOUR_LOCAL_IP}:27013" } ] })
This worked for me too
Im confused - Did you exec into the mongo node container in order to setup the cluster? e.g. follow this to the letter? https://gist.github.com/asoorm/7822cc742831639c93affd734e97ce4f#gistcomment-2707084
Because the docker containers for Mongo are in the same network. e.g.
mongo1
should be able to communicate with mongo2 via it's hostname & port.mongo2:27017
.If your mongo1 needs to go via the host machine (outside of docker) and come back into docker again, something clearly is not working.
@asoorm, yes, I followed the steps exactly. I found that the issue was from my host machine resolving the hostnames (mongo1, mongo2, mongo3).
I fixed this by adding the hostnames to /etc/hosts
:
sudo echo "127.0.0.1 mongo1
127.0.0.1 mongo2
127.0.0.1 mongo3" | sudo tee -a /etc/hosts
and then this worked as expected:
rs.initiate(
{
_id : 'rs0',
members: [
{ _id : 0, host : "mongo1:27017" },
{ _id : 1, host : "mongo2:27017" },
{ _id : 2, host : "mongo3:27017" }
]
}
)
zsf3 commented on Sep 5, 2020
why did you not mount any volumes? all the data in all of three mongodb databases will be destroyed when these containers are destroyed.
Author
asoorm commented on Sep 6, 2020
why did you not mount any volumes? all the data in all of three mongodb databases will be destroyed when these containers are destroyed.
Honestly - I posted the gist for my own purposes - and it happened to be helpful for others. It’s a local dev setup for testing purposes and it suited my requirements as is.
luco commented on Sep 9, 2020
I can initiate a replicaSet, but I can't access it via any client.
luco commented on Sep 9, 2020 •
@chidiwilliams where did you put these?
After writing the following under docker exec :
rs.initiate({
_id : 'rs0',
members: [
{ _id : 0, host : "{YOUR_LOCAL_IP}:27011" },
{ _id : 1, host : "{YOUR_LOCAL_IP}:27012" },
{ _id : 2, host : "{YOUR_LOCAL_IP}:27013" }
]
})
Don't I have to explicitly add the secondary mongo services by doing:
rs.add("mongo2:27012")
rs.add("mongo3:27013")
and should I not then initiate in both the secondary mongo services :
docker exec -it mongo2 /bin/bash
rs.slaveOk()
docker exec -it mongo3 /bin/bash
rs.slaveOk()
Haybu commented on Sep 29, 2020 •
I followed the steps below to run a simple replica set with one member for local development, :
1- run docker-compose up -d
with the below docker-compose file
version: "3.4"
services:
mongo1:
hostname: mongodb
container_name: mongodb
image: mongo:latest
environment:
MONGO_INITDB_DATABASE: {DATABASE NAME}
MONGO_REPLICA_SET_NAME: rs0
expose:
- 27017
ports:
- 27017:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
2- shell into the container: docker exec -it mongodb /bin/bash
3- execute mongo
command in the container's shell
4- register the replica set member by running
rs.initiate({
_id: "rs0",
version: 1,
members: [
{ _id: 0, host : "localhost:27017" }
]
}
)
5- use this URL to connect: mongodb://localhost:27017/{DATABASE NAME}?replicaSet=rs0
Follow the below compose file, no more manual changes
mongodb:
hostname: mongodb
container_name: mongodb
image: mongo:latest
environment:
MONGO_INITDB_DATABASE: moviebooking
MONGO_REPLICA_SET_NAME: rs0
volumes:
- ./mongo-initdb.d:/docker-entrypoint-initdb.d
expose:
- 27017
ports:
- "27017:27017"
restart: always
healthcheck:
test: test $$(echo "rs.initiate().ok || rs.slaveOk().ok || rs.status().ok" | mongo --quiet) -eq 1
interval: 10s
start_period: 30s
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
shell into the container: docker exec -it mongodb /bin/bash
you should see the below
Qiming-C commented on Nov 3, 2020 •
> rs.initiate({ ... _id : 'rs0', ... members: [ ... { _id : 0, host : "{YOUR_LOCAL_IP}:27717" }, ... { _id : 1, host : "{YOUR_LOCAL_IP}:27718" }, ... { _id : 2, host : "{YOUR_LOCAL_IP}:27720", arbiterOnly: true } ... ] ... }) { "operationTime" : Timestamp(0, 0), "ok" : 0, "errmsg" : "No host described in new configuration 1 for replica set rs0 maps to this node", "code" : 93, "codeName" : "InvalidReplicaSetConfig", "$clusterTime" : { "clusterTime" : Timestamp(0, 0), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } >
I am getting this error in window. I tested it in mac which works fine
for single
I followed the steps below to run a simple replica set with one member for local development which works fine
for work environment i need to configure three standalone system docker container mongo. can you please suggest what change i need to do
is it possible to configure MongoDB replication inside docker with three different standalone server system. I have configured in single server system its working fine when am trying to configure on three different machine, secondary system is not able to add. Please help me out.
Do not use x.509 Certificates or keyfile?
> rs.initiate({ ... _id : 'rs0', ... members: [ ... { _id : 0, host : "{YOUR_LOCAL_IP}:27717" }, ... { _id : 1, host : "{YOUR_LOCAL_IP}:27718" }, ... { _id : 2, host : "{YOUR_LOCAL_IP}:27720", arbiterOnly: true } ... ] ... }) { "operationTime" : Timestamp(0, 0), "ok" : 0, "errmsg" : "No host described in new configuration 1 for replica set rs0 maps to this node", "code" : 93, "codeName" : "InvalidReplicaSetConfig", "$clusterTime" : { "clusterTime" : Timestamp(0, 0), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } >
I am getting this error in window. I tested it in mac which works fine
I was able to solve the problem. Because I googled my ip address which is not configurable in mongoshell. running ipconfig to find the actual ipv4 address solve my issue
I resolved this problem using my local (host) IP:
rs.initiate({ _id : 'rs0', members: [ { _id : 0, host : "{YOUR_LOCAL_IP}:27011" }, { _id : 1, host : "{YOUR_LOCAL_IP}:27012" }, { _id : 2, host : "{YOUR_LOCAL_IP}:27013" } ] })
This worked for me too
Im confused - Did you exec into the mongo node container in order to setup the cluster? e.g. follow this to the letter? https://gist.github.com/asoorm/7822cc742831639c93affd734e97ce4f#gistcomment-2707084
Because the docker containers for Mongo are in the same network. e.g.mongo1
should be able to communicate with mongo2 via it's hostname & port.mongo2:27017
.
If your mongo1 needs to go via the host machine (outside of docker) and come back into docker again, something clearly is not working.@asoorm, yes, I followed the steps exactly. I found that the issue was from my host machine resolving the hostnames (mongo1, mongo2, mongo3).
I fixed this by adding the hostnames to
/etc/hosts
:sudo echo "127.0.0.1 mongo1 127.0.0.1 mongo2 127.0.0.1 mongo3" | sudo tee -a /etc/hosts
and then this worked as expected:
rs.initiate( { _id : 'rs0', members: [ { _id : 0, host : "mongo1:27017" }, { _id : 1, host : "mongo2:27017" }, { _id : 2, host : "mongo3:27017" } ] } )
This worked for me, thanks.
Follow the below compose file, no more manual changes
mongodb: hostname: mongodb container_name: mongodb image: mongo:latest environment: MONGO_INITDB_DATABASE: moviebooking MONGO_REPLICA_SET_NAME: rs0 volumes: - ./mongo-initdb.d:/docker-entrypoint-initdb.d expose: - 27017 ports: - "27017:27017" restart: always healthcheck: test: test $$(echo "rs.initiate().ok || rs.slaveOk().ok || rs.status().ok" | mongo --quiet) -eq 1 interval: 10s start_period: 30s entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
shell into the container:
docker exec -it mongodb /bin/bash
you should see the below
I know this has been here for awhile, but just doing this and liked the post by @mohansainani about doing this in a single docker image.
On a Mac, the only problem was that I was getting a error
Could not find host matching read preference { mode: \"nearest\" }
that indicated there was a network issue.
So.. I also used
sudo echo "127.0.0.1 mongodb" | sudo tee -a /etc/hosts
to make the host "findable"
After that I could connect with
mongo mongodb://localhost:27017/moviebooking?replicaSet=rs0
If that helps, bitnami/mongodb is a super good image.
mongodb:
image: bitnami/mongodb
environment:
MONGODB_REPLICA_SET_MODE: primary
ALLOW_EMPTY_PASSWORD: "yes"
I can run everything perfectly but I can not connect to the set by any client.
ghost commented on Oct 5, 2021 •
I can run everything perfectly but I can not connect to the set by any client.
i have same problem
hel;p
Error:
getaddrinfo ENOTFOUND mongodb-primary
ghost commented on Oct 5, 2021
If that helps, bitnami/mongodb is a super good image.
mongodb: image: bitnami/mongodb environment: MONGODB_REPLICA_SET_MODE: primary ALLOW_EMPTY_PASSWORD: "yes"
How did you configure the port
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK