skip to Main Content

I have a Flask app that runs without errors if I run python app.py locally. It says Running on http://127.0.0.1:5000, and if I just go to http://127.0.0.1:5000, everything works like it’s supposed to. The last line of app.py is app.run(debug=True).

Now I’m trying to run this app using Docker. In my Dockerfile, one of the lines is EXPOSE 8501.

I then build the image with docker build -t testimage .

Finally I execute docker run -it --expose=8501 -p 8501:8501 testimage, and again it says Running on http://127.0.0.1:5000.

My questions is :what do I have to enter in my browser to access the app that is running inside the Docker container?

2

Answers


  1. You’re exposing port 8501, but still run the flask app on port 5000. You can should either start the app on exposed port (you can do that using port argument to app.run) or expose a different port when running docker, matching the one app runs on.

    Login or Signup to reply.
  2. let’s this is your flask server code

    @app.route("/")
    def home():
        response=f"Flask Server > Hello World : Flask in Docker. Current Time:{time.strftime('%B %d, %Y %I:%M:%S %p')}"
        log_file(response)
        return response
    
    if __name__ == "__main__":
        app.run(host="0.0.0.0",debug=True,port=5000)
    

    so flask app is running at port 5000
    now this is docker file

    #run as main.py
    CMD ["python","flask_server/app.py"]
    

    now suppose you run docker run -it -p 5002:5000 image_name mapping port 5002 of the host(your machine) to port 5000 of docker. so on browser , open http://127.0.0.1:5002/
    for more info, you can refer my CICD repo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search