How to run Python Flask in a Docker container

I am trying to run the Python Flask web server in a docker container, but I cannot connect to the Flask server from the outside.

What I've done:

I created /temp/HelloFlask.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

I launched a docker container with a container of 5000 that was mapped to host port 5000, and with ~ / temp set to / temp

docker run -it -p 5000:5000 -v ~/temp:/temp --name tf gcr.io/tensorflow/tensorflow:latest-devel

Inside the docker container, I checked and ran HelloFlask.py

cd /temp
pip install Flask
python HelloFlask.py &

I confirmed that the server was available in the container

[root@de8b6996b540:/temp# curl localhost:5000
127.0.0.1 - - [22/Sep/2016 17:41:48] "GET / HTTP/1.1" 200 -
Hello World!

I am using Docker Version 1.12.1 (build: 12133), which provides container ports on localhost, so I have to access localhost:5000on my Mac, outside the container, but I can’t connect.

, , Docker localhost, nginx, mac quickstart, localhost .

+4
1

app.run(host='0.0.0.0').

Flask . , - . host='0.0.0.0' IP-. .

+14

Source: https://habr.com/ru/post/1655518/


All Articles