I'm trying to setup a Flask <-> MongoDB <-> mongo_express application. I have 3 containers defined in my docker-compose.yml, and I start them successfully. However, while the Mongo part is OK (I can access the DB via the express api at localhost:8081), Flask can't access the DB.
What I'm looking for:
- I want to be able to send requests from host machine (or any other
in the network) to Flask (running on 0.0.0.0:5000), from Flask to DB
(running on localhost:27017, accessed using pymongowrapper).
- Also, I want to be able to have access to mongo_express (on localhost:8081) and from it to the DB [This part is already working!]
In order to debug it, I removed the Flask container from the docker-compose.yml, restarted, and run it locally, and voila! everything works (meaning my pytest runs are ok - I have a test where I send a request to the Flask server, which in turn, uses the pymongo wrapper to access the DB, and return data from it to the client).
I guess my network configuration is flawed, but I don't understand where.
Here is my docker-compose.yml (Flask is commented out, since it is currently running locally):
version: "3.8"
services:
    # mgmt_server:
    #     build: ./server # Dockerfile just copies py files, installs pip requirements, and runs "python server.py"
    #     container_name: mgmt_server
    #     restart: always
    #     networks:
    #         - backend   # Connect to flask
    #     ports:
    #         - "5000:5000"
    mongo:
        image: mongo:latest
        container_name: mongodb
        restart: always
        environment:
            - MONGO_INITDB_ROOT_USERNAME=root
            - MONGO_INITDB_ROOT_PASSWORD=pass
        volumes:
            - /data/db:/data/db
        networks:
            - backend   # Connect to flask
            - frontend  # Connect to mongo_express
        ports:
            - "27017:27017"
    mongo-express:
        image: mongo-express:latest
        container_name: mongo_express
        restart: always
        environment:
            - ME_CONFIG_MONGODB_SERVER=mongo
            - ME_CONFIG_MONGODB_PORT=27017
            - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
            - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
            - ME_CONFIG_MONGODB_ADMINUSERNAME=root
            - ME_CONFIG_MONGODB_ADMINPASSWORD=pass
            # Uncomment if a secure login via browser is required
            # - ME_CONFIG_BASICAUTH_USERNAME=root
            # - ME_CONFIG_BASICAUTH_PASSWORD=pass
        links:
            - mongo
        networks:
            - frontend  # Connect to mongo_express
        ports:
            - 8081:8081
networks:
    backend:
        driver: bridge
    frontend: 
        driver: bridge
$ docker network ls # When mgmt_server is run locally
NETWORK ID          NAME                 DRIVER              SCOPE
10577560a149        bridge               bridge              local
a037a11e12bb        host                 host                local
b153eea9db12        node_mgmt_backend    bridge              local
c8e1b58ffb44        node_mgmt_frontend   bridge              local
4f7b75b5695a        none                 null                local
