Docker Error in MongoDB (demo setup)

Since this discourse seems to just close topics and not reopen them I’m making a new one to help out sudharsan_v in 1054

The issue with ...{“code”:26,“codeName”:“NamespaceNotFound”,“errmsg”:... is because the mongosetup doesn’t seem to work in the docker-compose example. It didn’t for me on Linux trying this out so I took a search and only found this unanswered question.

For posterity of the old web days and trying to help people who search in the future. Go run the mongo setup command manually. You can even remove the entire section from that docker-compose.yml since it doesn’t work properly.

Login to your docker container to run commands with:
docker ps to find the container name
docker exec -it demo-mongodb-1 bash if it’s the default container name
mongosh --host mongodb:27017 to get to the mongo shell
use openflow to use the openflow db
rs.status() likely shows an error with no config
config = {"_id" : "rs0", "members" : [ { "_id" : 0, "host" : "mongodb:27017" } ] }; set the config from whatever mongo needs, the default is 0 replicas in this demo
rs.initiate(config); initiate
rs.status() confirms things are working now
exit (twice) gets you back out to the command line

Now going to http://localhost.openiap.io should no longer give you Bad Gateway if it was just the rs config issue.

You can close this topic, I’m just trying to help… and Allan_Zimmermann if you’re trying to have a nice searchable discourse I’d recommend permitting reopening of topics for just this use case.

1 Like

I’m not sure I agree with mongosetup doesn’t seem to work. I’m using it all the time on Linux and macOS, and with different versions of MongoDB. But it is true, sometimes you need to do a Docker Compose up, wait a little while, then do Docker Compose down, and then Docker Compose up. The first time setup runs, it needs to set up the replica set, but for some reason, especially on older computers, it takes a while for MongoDB to start up the first time. So, the API fails to connect to a working MongoDB, and nothing “seems to work.” Doing a down and up will rerun the process, and the API can connect after a few tries.

Once the replica set is working, you can safely remove or comment out the dbsetup part, then it’s “just noise.”

You can also completely skip using the mongosetup section and set up the replica set manually as described by @some1has2 and great details.

Others might prefer to use a bash script to do it, as explained here. All perfect valid and good ways to initialize the replica set.

https://www.mongodb.com/community/forums/t/replicaset-via-mongod-conf-in-docker-causes-init-issues/291701

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.