I have a flask app, with Flask RestX, that works fine with flask run
but has import issues with Docker. I've seen some questions and answers about this but they all involve Gunicorn, but I'm not using Gunicorn.
Here is my folder structure. I'm running the docker-compose
file which uses the flask.dev.dockerfile
as well as a Redis container.
So, I want to use my redis wrapper, RedisDatabase
, from my database.py
file. PyCharm inserts this as
from ivd_app.database import RedisDatabase
And the rest of the __init__.py
file, in summary:
from flask import Flask, request
from flask_cors import CORS
from flask_restx import Api, Resource, fields
def create_app(db=None):
app = Flask(__name__, instance_relative_config=True)
if (db is None):
db = RedisDatabase()
api = Api(app, version='0.0.1', title='IVD')
config_endpoint = api.namespace(
'config', description='APIs to Send configuration to and from the Front End'
)
config_model = api.model('Configuration', {
# the model
})
@config_endpoint.route('/')
class ConfigurationEndpoint(Resource):
@config_endpoint.expect(config_model, validate=True)
def put(self):
# the put endpoint
def get(self):
# the get endpoint
return app
# if __name__ == "__main__":
# create_app().run(host="0.0.0.0", debug=True)
When I use CMD flask run
from the dockerfile after setting the environment variables, the application runs. But for some reason I can't access the flask server, so instead I uncomment the lines at the bottom, since I know those work, and use CMD python ivd_app/__init__.py
in the Dockerfile. But the container exits:
flask_1 | Traceback (most recent call last):
flask_1 | File "ivd_app/__init__.py", line 38, in <module>
flask_1 | from ivd_app.database import RedisDatabase
flask_1 | ModuleNotFoundError: No module named 'ivd_app'
If I remove the import statement and put the RedisDatabase
class in the __init__.py
file, the entire app works, of course. But I want to separate it into a different file.
How do I fix this?