I can connect to my mongodb in a container, but when i try with my service in another container i get a connection refused

I followed the directions to create a mongoDb in a container here (How to Deploy and Manage MongoDB with Docker)

In my code in VSCODE i am able to connect to the mongod in the container and pull and insert data in different dbs and collections; however I deploy this service in a container and and use postman to hit the endpoint in the service. It hits the service but when its doing a db.collection.find() i get the following error:

pymongo.errors.ServerSelectionTimeoutError: 0.0.0.0:27017: [Errno 111] Connection refused

1 Like

Your app and db need to share a docker network. Beyond that I would suggest looking at the docker documentation. Docker compose can help you create application stacks.

I talked to a few other people and they also suggested the docker network. I tried to ping the the other container’s ip and didn’t get a response back. Based on the IP address structure they look to be on the same subnet.

A docker network inspect will show what containers are on the same network.

An example in 3 commands:

$ docker network create mdb
821e11d5b1d05fd0ece3985e19cb05f98b9fa2c1cc5caaf9f9f8e3f51722a353
$ docker run -d --network mdb --name mongo mongo:4.4
be432cf4c7e1abeed256640aecb3dcfdfa07ef9ddd589bdcbf5acc731803ea20
$ docker run --rm --network mdb -it mongo:4.4 mongo --quiet  mongo/local --eval 'db.startup_log.find({},{startTime:1, "buildinfo.version":1})'
{ "_id" : "be432cf4c7e1-1608161225828", "startTime" : ISODate("2020-12-16T23:27:05Z"), "buildinfo" : { "version" : "4.4.1" } }