0

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'}
1
  • 1
    Usually it should work, I don't see any reason why it fails. Maybe try db.getSiblingDB('quiz-db').createUser(...) Commented Dec 29, 2022 at 9:10

1 Answer 1

2

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

Use authSource=admin in the URI

Sign up to request clarification or add additional context in comments.

6 Comments

Tried that too but it doesn't work.
Check the logs, it should show the connection and authentication attempt.
The logs show this: "error":"UserNotFound: Could not find user \"quiz-admin\" for db \"admin\""}} And I know that do be correct, I can't login to admin with those credentials, cause the user is created inside the quiz-db database. Maybe I need to create the user in admin instead?
That would be worth a try
Ok seems to be working if I create the user inside the admin db. Thanks for the help!
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.