Home > Net >  Failed Authentication when connecting with Flask through PyMongo to MongoDB in Docker Compose
Failed Authentication when connecting with Flask through PyMongo to MongoDB in Docker Compose

Time:12-29

I'm using Docker Compose and trying to make two containers talk to each other. One runs a MongoDB database and the other one is a Flask app that needs to read data from the first one using PyMongo.

The Mongo image is defined with the following Dockerfile:


FROM mongo:6.0

ENV MONGO_INITDB_ROOT_USERNAME admin
ENV MONGO_INITDB_ROOT_PASSWORD admin-pwd
ENV MONGO_INITDB_DATABASE admin

COPY mongo-init.js /docker-entrypoint-initdb.d/

EXPOSE 27017

And my data is loaded through the following mongo-init.js script:

db.auth('admin','admin-pwd')

db = db.getSiblingDB('quiz-db')

db.createUser({
    user: 'quiz-admin',
    pwd: 'quiz-pwd',
    roles: [
        {
            role: 'readWrite',
            db: 'quiz-db'
        }
    ]
});

db.createCollection('questions');

db.questions.insertMany([
    {
        question: "Do you like sushi?",
        answers: {
            0:"Yes",
            1:"No",
            2:"Maybe"
        }
    }
]);

The Flask app is pretty straightforward. I'll skip the Dockerfile for this one as I don't think it's important to the issue. I try to connect to the database with the following code:

from flask import Flask, render_template
from pymongo import MongoClient

app = Flask(__name__)

MONGO_HOST = "questions-db"
MONGO_PORT = "27017"
MONGO_DB = "quiz-db"
MONGO_USER = "quiz-admin"
MONGO_PASS = "quiz-pwd"
uri = "mongodb://{}:{}@{}:{}/{}?authSource=quiz-db".format(MONGO_USER, MONGO_PASS, MONGO_HOST, MONGO_PORT, MONGO_DB)

client = MongoClient(uri)
db=client["quiz-db"]
questions=list(db["questions"].find())

I'm not an expert when it comes to Mongo, but I've set authSource to 'quiz-db' since that's the database where I've created the user in the 'mongo-init.js' script. I tried to run the database container alone and I did successfully log in using mongosh with the user 'quiz-db'. All the data is there and everything works fine.

The problem is only coming up when trying to connect from the Flask app. Here's my Docker compose file:

version: '3.9'

services:
    #Flask App
    app:
        build: ./app
        ports:
            - "8000:5000"
        depends_on:
            - "questions-db"
        networks:
            - mongo-net
    
    #Mongo Database
    questions-db:
        build: ./questions_db
        hostname: questions-db
        container_name: questions-db
        ports:
            - "27017:27017"
        networks:
            - mongo-net

networks:
    mongo-net:
        driver: bridge

When I run 'docker compose up' I get the following error on the Flask container startup:

pymongo.errors.OperationFailure: command find requires authentication
full error: {'ok': 0.0, 'errmsg': 'command find requires authentication', 'code': 13, 'codeName': 'Unauthorized'}

CodePudding user response:

MongoDB stores all user credentials in the admin database, unless you are using a really ancient version.

Use authSource=admin in the URI

  • Related