r/mongodb•
fs50

Having trouble connecting to docker mongodb with pymongo

I am trying to spin up a local docker mongodb, but I keep getting auth errors trying to call to it.

Here is some python code I have: def create_app(): uri = "mongodb://root:rootpassword@localhost:27017/?authSource=admin"
database.client = database.init_db(uri) app = Flask(name)
app.register_blueprint(user_bp) return app

And my compose file services: mongodb: image: mongo:latest container_name: testing ports: - "27017:27017" environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: rootpassword

I test this calling /users to return some users. When I connect to a cloud mongodb cluster uri, it works as intended, but switching the uri to the docker one gives me an auth error.

I spin up and down the dockers with these commands:

"docker-compose -f docker-compose.test.yaml -p test_database-container up --build "

"docker-compose -f docker-compose.test.yaml -p test_database-container down -v"

Anyone know what's wrong?

Posted Aug 12, 2025, 5:33 PM UTC

11
Loading comments…