I made a super simple python app which I am using to learn about docker and K8s, but I am having issues with the authentication, as I get this error when the app starts:
app_1 | pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', 'code': 18, 'codeName': 'AuthenticationFailed'}
Here is my app, I have tried with both the comment and the uncomment configuration, as I saw in this thread from SO here , but none of them works:
from flask import Flask, jsonify
from flask_pymongo import PyMongo
import os
import json_logging, logging, sys
app = Flask(__name__)
json_logging.init_flask(enable_json=True)
json_logging.init_request_instrument(app)
logger = logging.getLogger("mongodb-service")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
# app.config['MONGO_HOST'] = os.environ['MONGODB_HOSTNAME']
# app.config['MONGO_DBNAME'] = os.environ['MONGODB_DATABASE']
# app.config['MONGO_USERNAME'] = os.environ['MONGODB_USERNAME']
# app.config['MONGO_PASSWORD'] = os.environ['MONGODB_PASSWORD']
# app.config['MONGO_AUTH_SOURCE'] = 'admin'
# mongo = PyMongo(app, config_prefix='MONGO')
app.config["MONGO_URI"] = 'mongodb://' + os.environ['MONGODB_USERNAME'] + ':' + os.environ['MONGODB_PASSWORD'] + '@' + os.environ['MONGODB_HOSTNAME'] + ':27017/' + os.environ['MONGODB_DATABASE']
mongo = PyMongo(app)
db = mongo.db
@app.route('/')
def ping_server():
return "Welcome to the second best website ever(v2) "
@app.route('/cars')
def get_stored_cars():
_cars = db.car_tb.find()
cars = [{"brand": car["brand"], "model": car["model"]} for car in _cars]
return jsonify({"cars": cars})
......
I am connecting it to a mongodb container I create in my docker-compose:
version: "3.7"
services:
app:
container_name: cars_app
image: latalavera/flask-app:20.1
build: .
environment:
APP_ENV: "dev"
APP_DEBUG: "False"
MONGODB_DATABASE: cars_db
MONGODB_USERNAME: root
MONGODB_PASSWORD: pass
MONGODB_HOSTNAME: mongodb-flask
ports:
- ${APP_PORT}:${APP_PORT}
depends_on:
- mongodb
networks:
- app_network
mongodb:
container_name: cars_db
image: mongo:4.0
hostname: mongodb-flask
env_file:
- db-env-vars.env
volumes:
- ./init-db.js:/docker-entrypoint-initdb.d/init-db.js:ro
- ./mongo-volume:/data/db
ports:
- ${DB_PORT}:${DB_PORT}
networks:
- app_network
networks:
app_network:
name: app_net
driver: bridge
but I cannot figure out what is the issue after going through the documentation and several threads here in SO
so I found a way to make it work, I just defined the connection like this:
def get_db():
client = MongoClient(host=os.environ['MONGODB_HOSTNAME'],
port=27017,
username=os.environ['MONGODB_USERNAME'],
password=os.environ['MONGODB_PASSWORD'],
authSource="admin")
db = client[os.environ['MONGODB_DATABASE']]
return db
then defined the variables in the env filed in the dockerfile as above
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.