Docker Community Forums

Share and learn in the Docker community.

Problem with flask running

Hi!
I put this code (python) in my container:

root@e9bcddeb9491:/# cat > /opt/app.py
import os
from flask import Flask
app = Flask(__name__)

@app.route("/")
def main():
    return "Welcome!"

@app.route('/how are you')
def hello():
    return 'I am good, how about you?'

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080, Debug=True)
^C

and then I ran

FLASK_APP=app.py flask run --host=0.0.0.0

And I got this erorr:

root@e9bcddeb9491:/# FLASK_APP=app.py flask run --host=0.0.0.0
 * Serving Flask app 'app.py' (lazy loading)
 * 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: flask run [OPTIONS]
Try 'flask run --help' for help.

**Error: Could not import 'app'.**

If you know what the problem is- It will help me a lot,
thank you!

Your code seems working, I tried. how did you installed flask and python? Which Python version are you using?

I would also check if you use the same app.py as you shared here, because I don’t understand why Python wants to import “app”.

This is my working Dockerfile

FROM python:3.8

RUN python3 -m pip install flask
RUN mkdir /app
WORKDIR /app
COPY app.py /app
ENV FLASK_APP=app.py
CMD ["flask", "run", "--host=0.0.0.0"]

Hi,
Thank you very much!