Reputation: 81
I am trying to dockerize my Flask API. As soon as I try to start my image I receive the message:
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: python -m flask run [OPTIONS]
Try 'python -m flask run --help' for help.
Error: While importing 'app', an ImportError was raised.
If I am starting the Flask App with my terminal python -m flask run
everything works like intended.
And right now I am stuck on this problem.
Here is my Code:
from flask import Flask
from bson import json_util
from flask_pymongo import PyMongo
from flask_cors import CORS
import json
app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://194.163.147.192:27017/test"
CORS(app)
mongo = PyMongo(app)
def parse_json(data):
return json.loads(json_util.dumps(data))
@app.route('/')
def home():
return 'Hello'
@app.route('/residential', methods=['GET'])
def find_residential(): # put application's code here
test = mongo.db.acs.find_one({"name": "Residential"})
response = Flask.jsonify(parse_json(test))
response.headers.add('Access-Control-Allow-Origin', '*')
return response
@app.route('/commercial', methods=['GET'])
def find_commercial(): # put application's code here
test = mongo.db.acs.find_one({"name": "Commercial"})
response = Flask.jsonify(parse_json(test))
response.headers.add('Access-Control-Allow-Origin', '*')
return response
@app.route('/healthcare', methods=['GET'])
def find_health_care(): # put application's code here
test = mongo.db.acs.find_one({"name": "Health Care"})
response = Flask.jsonify(parse_json(test))
response.headers.add('Access-Control-Allow-Origin', '*')
return response
@app.route('/germany', methods=['GET'])
def find_germany():
test = mongo.db.germanies.find_one()
response = Flask.jsonify(parse_json(test))
response.headers.add('Access-Control-Allow-Origin', '*')
return response
if __name__ == '__main__':
app.debug = False
app.run()
My requirements.txt looks like this
bson==0.5.10
click==8.0.3
colorama==0.4.4
Flask==2.0.2
Flask-Cors==3.0.10
Flask-PyMongo==2.3.0
itsdangerous==2.0.1
Jinja2==3.0.2
MarkupSafe==2.0.1
pymongo==3.12.1
python-dateutil==2.8.2
six==1.16.0
Werkzeug==2.0.2
My Dockerfile looks like this
FROM python:3.8-slim-buster
WORKDIR /api
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
I am thankful for any help :)
Project structure is like:
API
L venv
L app.py
L Dockerfile
L requirements.txt
Upvotes: 3
Views: 3489
Reputation: 3
Not sure if it will help... but I had the same issue too and I've just fixed it changing my entrypoint.sh
file.
Before I was starting the flask app using:
flask run --host 0.0.0.0 --port 5000
And now I'm using gunicorn:
gunicorn -b 0.0.0.0:5000 app:app
So I had to add gunicorn
in the requirements.txt
file too.
Upvotes: 0
Reputation: 21210
Your issue is, I think, with your requirements file. In that you include bson
as a dependency, which is also included in the pymongo
library. See this question. Removing it seems to solve the issue:
~/tmp/so_q $ docker build -t myimage . 8s nathanielford@nford 20:51:04
Sending build context to Docker daemon 5.12kB
...
Successfully tagged myimage:latest
~/tmp/so_q $ docker run myimage 13s nathanielford@nford 20:51:26
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://192.168.9.2:5000/ (Press CTRL+C to quit)
Upvotes: 1