0

I am trying to set up a flask app using nginx reverse proxy and my flask should be talking to mongodb backend but I can seem to be able to get mongodb to setup the default admin user so. I can connect to the database but when I try to authenticate using the admin user credentials I get "authentication failure error"!

Here is my docker-compose.yaml file:

version: '3.7'
services:

  mongodb:
      env_file:
          - .env
      image: mongo:5.0.6
      ports:
        - "30000:27017"
      container_name: groupkash-mongodb
      restart: always
  #    command: echo 'Hello'
      environment:
        - MONGODB_DATA_DIR=/data/db
        - MONDODB_LOG_DIR=/dev/null
        - MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME}
        - MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
      volumes:
        - ./mongodbdata:/data/db
      networks:
        - backend
  flask:
    env_file:
      - .env
    build:
      context: .
      dockerfile: Dockerfile
    container_name: groupkash-flask
    image: groupkash-gunicorn-flask:v1.0
    restart: always
    environment:
      - APP_ENV=${APP_ENV}
      - APP_DEBUG=${APP_DEBUG}
      - APP_PORT=${APP_PORT}
      - MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME}
      - MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
#      - MONGO_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@localhost:30000/
    volumes:
      - ./appdata:/app/data
    depends_on:
      - mongodb
    networks:
      - frontend
      - backend

  webserver:
    build:
      context: nginx
      dockerfile: Dockerfile
    image: groupkash-webserver:v1.0
    container_name: groupkash-webserver
    restart: always
    environment:
      APP_ENV: "prod"
      APP_NAME: "webserver"
      APP_DEBUG: "true"
      SERVICE_NAME: "webserver"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginxdata:/var/log/nginx
    depends_on:
      - flask
    networks:
      - frontend

networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge

volumes:
  mongodbdata:
    driver: local
  appdata:
    driver: local
  nginxdata:
    driver: local

I am passing the environment values via a "gunicorn_config.py" file. This is my connection string used:

MONGO_URI=mongodb://user:[email protected]:30000/database 

Error message:

Error: 172.21.0.2:30000: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 627bbf91d21f9dbb2f1e4267, topology_type: Unknown, servers: [<ServerDescription ('172.21.0.2', 30000) server_type: Unknown, rtt: None, error=AutoReconnect('172.21.0.2:30000: [Errno 111] Connection refused')>]>]

And the docker network inspect command shows mongodb and flask are on the same network:

"Containers": {
            "8e5d145e88ea35a0bf974f8556ad37739853b73803d8f002c82051a5332cdc6a": {
                "Name": "groupkash-flask",
                "EndpointID": "401c504ab71f5b5f8c97696bbb6635ff453dce158a382c5bf3706240b8222ffa",
                "MacAddress": "02:42:ac:15:00:03",
                "IPv4Address": "172.21.0.3/16",
                "IPv6Address": ""
            },
            "a9612c8ec181d5b574e7bc9067fa93d71b77b0d1de6a1a86c6706a028d52017d": {
                "Name": "groupkash-mongodb",
                "EndpointID": "3680f8eaf19cf42223f97ac90a3993b95848f14d69316c501e15d9549dc16de1",
                "MacAddress": "02:42:ac:15:00:02",
                "IPv4Address": "172.21.0.2/16",
                "IPv6Address": ""
            }
        },

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.