I am using docker-compose to run 2 images: a flask webserver and a mongodb database.
If I launch just the mongodb database container (official image) and run the flask app locally it works (connecting to localhost:27017). I can also access to the mongodb at localhost:27017 with the graphical interface MongodbCompass.
But when I launch the docker-compose with the 2 services, my connection is refused: pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
From the containerized flask app I have tried to connect both to localhost:27017 and mongo:27017 (this is the name of the service) with error. What makes me crazy is that in this case, I am still able to connect to localhost:27017 with MongodbCompass.
This is my docker-compose file:
version: '3'
services:
mongo:
image: mongo
volumes:
- /mnt/usb/data:/data/db
ports:
- 27017:27017
frontend:
build: frontend/.
ports:
- 80:8080
depends_on:
- mongo
frontend
service starts. Read this stackoverflow.com/questions/31746182/… to learn how to delayfrontend
service start only aftermongo
service is up. – norbjddepends_on
does not guarantee thatmongo
database is ready to accept connections whenfrontend
starts. It only guarantees the startup order (mongo
, thenfrontend
) : take a look at the example in the docs (docs.docker.com/compose/compose-file/#/dependson#depends_on) :depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.
– norbjd