Failed to access MongoDB in container through Flask application
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.
python mongodb docker flask mongoengine
add a comment |
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.
python mongodb docker flask mongoengine
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 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? 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
add a comment |
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.
python mongodb docker flask mongoengine
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.
python mongodb docker flask mongoengine
python mongodb docker flask mongoengine
asked Nov 15 '18 at 16:48
T.TosT.Tos
25
25
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 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? 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
add a comment |
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 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? 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
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
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
@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 hub.docker.com/r/tiangolo/uwsgi-nginx-flask
– T.Tos
Nov 15 '18 at 23:26
The WSGI options are unchanged from this image 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? 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
Possible duplicate of 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
@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
add a comment |
2 Answers
2
active
oldest
votes
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.
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 thisapp.config["MONGO_URI"] = "mongodb://mongo:27017/database-name"
and removeapp.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 docs.mongodb.com/manual/mongo). "mongo --host mongo" does not work.
– T.Tos
Nov 16 '18 at 10:27
|
show 2 more comments
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324218%2ffailed-to-access-mongodb-in-container-through-flask-application%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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 thisapp.config["MONGO_URI"] = "mongodb://mongo:27017/database-name"
and removeapp.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 docs.mongodb.com/manual/mongo). "mongo --host mongo" does not work.
– T.Tos
Nov 16 '18 at 10:27
|
show 2 more comments
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.
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 thisapp.config["MONGO_URI"] = "mongodb://mongo:27017/database-name"
and removeapp.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 docs.mongodb.com/manual/mongo). "mongo --host mongo" does not work.
– T.Tos
Nov 16 '18 at 10:27
|
show 2 more comments
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.
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.
answered Nov 15 '18 at 17:29
SiyuSiyu
2,5141725
2,5141725
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 thisapp.config["MONGO_URI"] = "mongodb://mongo:27017/database-name"
and removeapp.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 docs.mongodb.com/manual/mongo). "mongo --host mongo" does not work.
– T.Tos
Nov 16 '18 at 10:27
|
show 2 more comments
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 thisapp.config["MONGO_URI"] = "mongodb://mongo:27017/database-name"
and removeapp.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 docs.mongodb.com/manual/mongo). "mongo --host mongo" does not work.
– T.Tos
Nov 16 '18 at 10:27
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
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
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
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
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 docs.mongodb.com/manual/mongo). "mongo --host mongo" does not work.
– T.Tos
Nov 16 '18 at 10:27
"mongo --host localhost" and "mongo --host e63206b1b9b9" do work to connect me to the mongo shell (see docs.mongodb.com/manual/mongo). "mongo --host mongo" does not work.
– T.Tos
Nov 16 '18 at 10:27
|
show 2 more comments
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 18 '18 at 12:23
T.TosT.Tos
25
25
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324218%2ffailed-to-access-mongodb-in-container-through-flask-application%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 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? 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