-1

So guys n gals, hope you can help me.

So i'm diving into docker containers right now and i try to port my application (flask+mongodb) into two seperate containers. MongoDB container is set up and works great, running a mongoexpress container with a link gives me the access to the database i wished. Now i ported my Flask application and the login page loads fine (so the docker port itself works) but even though i run the container with a link to my database container, i get a server error when i try to load content from the database.

The logs give me the following traceback:

File "/usr/local/lib/python3.7/site-packages/mongoengine/queryset/manager.py", line 37, in __get__
    queryset = queryset_class(owner, owner._get_collection())
File "/usr/local/lib/python3.7/site-packages/mongoengine/document.py", line 190, in _get_collection
    db = cls._get_db()
File "/usr/local/lib/python3.7/site-packages/mongoengine/document.py", line 179, in _get_db
    return get_db(cls._meta.get('db_alias', DEFAULT_CONNECTION_NAME))
File "/usr/local/lib/python3.7/site-packages/mongoengine/connection.py", line 241, in get_db
    db = conn[conn_settings['name']]
File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1323, in __getitem__
    return database.Database(self, name)
File "/usr/local/lib/python3.7/site-packages/pymongo/database.py", line 107, in __init__
    "of %s" % (string_type.__name__,))
TypeError: name must be an instance of str

So my Flask-MongoEngine is set up like:

app = Flask(__name__)
app.config["MONGODB_DB"] = 'database-name'
db = MongoEngine(app)

Basically my database should be available on localhost at the standard port 27017. Is there anything i have to consider when connecting with Flask to the container or should the link not already expose the ports as expected?mongoexpress works and has access, so it has to be a problem with my setup.

T.Tos
  • 67
  • 1
  • 9
  • I'm not so sure about the below answer. Can you post the config info in your flask app? – Robert Moskal Nov 15 '18 at 18:02
  • @RobertMoskal I actually don't know what the config info is. Where can i find it? – T.Tos Nov 15 '18 at 23:20
  • The WSGI options are unchanged from this image https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/ – T.Tos Nov 15 '18 at 23:26
  • Possible duplicate of [how to link container in docker?](https://stackoverflow.com/questions/41768157/how-to-link-container-in-docker) I think you just need to follow the instructions in there. Or better still use docker-compose – Robert Moskal Nov 16 '18 at 16:12
  • @RobertMoskal User defined networks are the way to go! Solved my problem outside of the suggestions. As it seems, my configuration for Flask was simply not called. – T.Tos Nov 18 '18 at 12:21

2 Answers2

0

At least put this in your settings:

app.config['MONGODB_HOST'] = 'mongo'

The raison why your mongoexpress works is it looks for mongo on host named mongo by default, so when you do docker run -it --rm -p 8081:8081 --link YOUR_MONGODB_CONTAINER:mongo mongo-express it can find the linked mongo instance. However in flask-mongoengine, the host is default to localhost.

Siyu
  • 11,187
  • 4
  • 43
  • 55
  • Good Idea! My host in mongoexpress seems to be "e63206b1b9b9" but that does also not work for my flask application. – T.Tos Nov 15 '18 at 23:03
  • can you try this `app.config["MONGO_URI"] = "mongodb://mongo:27017/database-name"` and remove `app.config["MONGODB_*` – Siyu Nov 15 '18 at 23:13
  • Unsuccessful. So through shell i can access mongod processes on localhost and "e63206b1b9b9". mongo does not work. – T.Tos Nov 15 '18 at 23:27
  • what do you mean access mongod process through shell? mongo does not work? – Siyu Nov 15 '18 at 23:29
  • "mongo --host localhost" and "mongo --host e63206b1b9b9" do work to connect me to the mongo shell (see https://docs.mongodb.com/manual/mongo/). "mongo --host mongo" does not work. – T.Tos Nov 16 '18 at 10:27
  • where did you run these? on the host I suppose? try `mongo --host mongo` inside the container – Siyu Nov 16 '18 at 10:43
  • i'm running the container with the bash keyword. As i said, --host mongo does not work. Thank you i solved my problem. – T.Tos Nov 18 '18 at 12:18
0

In the end things were simpler than i thought. As i migrated my code from Apache2 to Nginx, the flow of my code changed without me noticing it. So my configuration lines for Flask were simply not called.

T.Tos
  • 67
  • 1
  • 9