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