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!

Hi rimelek, I am also facing the same problem, I want to ask, that you modify the docker file, can you tell me why you wrote From python:3.8, didn’t we have to write From ubuntu?

You don’t “have to” write anything. I used the python image because that contains python and I wanted to use a specific python version. You can use Ubuntu if you add the installation of Python into your Dockerfile. If you want to make sure you will always have the same version of Python, you probably want to use the python image, because that is easier than downloading and compiling Python from source based on an other image.

Thank you very much fo explaining @rimelek

One more thing is that it is successfully built, and it is showing that it is running on these IPs as shown in the picture. But when I access those links, it isn’t showing me anything.

  • As I am new to docker, I think so it is a silly mistake somewhere, did you know what wrong I did that

Without sharing how you started the container I can’t say much, but it is important to know that every IP address shown by processes inside the container is inside the container. You will not be able to acces those. 127.0.0.1 is the loopback interface of the container not your host. 172.17.0.4 is the Iocal P address of the container which you can access from outside the container but only if you are not running Docker Desktop which runs everything in a virtual machine.