Running Flask app in a Docker container

2019-03-27 22:03发布

I've built a Docker image containing a simple Flask test app:

from flask import Flask 

app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0')

using the Dockerfile:

FROM ubuntu:latest
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY . /app
WORKDIR /app
RUN pip install -r /app/requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]

The Docker image was built using docker build -t flask-app . and it has been successfully created:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
flask-app           latest              fab0d79fd7ac        8 minutes ago       642.2 MB
ubuntu              latest              104bec311bcd        5 weeks ago         129 MB

and I've run it using:

$ docker run -d -p 5000:5000 flask-app
e92b249dd02ca44489069b783fa9be713c7a14ea893061194e37c80f16d8c931

I'm assuming that I can test the app by pointing the browser to http://localhost:5000/ but I get a timeout. What could be going wrong?

5条回答
男人必须洒脱
2楼-- · 2019-03-27 22:03

Everything looks good from here. Let's do some debugging.

Let's begin by starting our container with a name: docker run -d -p 5000:5000 --name flask-app-test flask-app

You can also avoid the -d flag and get the logs right onto your screen.

First check if the Flask app is really running or maybe it has crashed. Start with a simple docker ps (see if you notice anything strange there) followed by docker logs flask-app-test (these will be the logs of the Flask app, has it crashed? is there aything strange?)

If everything looks good until here, then it's time to enter the container. Let's try with docker exec -ti flask-app-test bash. Once you are in, install wget by apt-get install wget and then test if the webserver works from the inside by doing wget http://localhost:5000 and by cat-ting the hopefully downloaded file (ls followed by cat file.html, or whatever it's called).

查看更多
够拽才男人
3楼-- · 2019-03-27 22:05

I did the following steps on my system:

  1. Copied your flask code and your Dockerfile
  2. Created a requirements.txt file with flask on the first line
  3. Built the image and run it, as you have done it

It works fine with me:

$ curl localhost:5000
Hello World!

The docker process running no problem and the logs checkout fine:

  $ docker ps
  CONTAINER ID        IMAGE               COMMAND             CREATED               STATUS              PORTS                    NAMES
  0c0228a88719        flask-app           "python app.py"     25 seconds ago      Up 24 seconds       0.0.0.0:5000->5000/tcp   frosty_borg

$ docker logs 0c0228a88719
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 216-035-222
172.17.0.1 - - [22/Jan/2017 12:56:37] "GET / HTTP/1.1" 200 -

When you start you docker container, check to see if it is listening port 5000. For example, on my system:

     $ netstat -atn | grep 5000
     tcp6       0      0  ::1.5000               *.*                    LISTEN     
     tcp4       0      0  *.5000                 *.*                    LISTEN     
     $ docker stop 0c0228a88719
0c0228a88719
     $ netstat -atn | grep 5000
     $ # stops listening

Importantly, how you running dpcker, is it being run natively on a linux desktop machine? Or are you running it within a Linux Virtual Machine on a Mac/Windows host (through a service like docker machine or other method)? If so then the problem could be that the IP you should be accessing is not localhost, but rather the IP address of the Virtual Machine.

查看更多
甜甜的少女心
4楼-- · 2019-03-27 22:14

Missing this part in Dockerfile

COPY ./requirements.txt /app/requirements.txt

Add this line to your Dockerfile before

app.py

from flask import Flask
app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0')

requirements.txt

Flask==0.10.1
#or it can be any version

Dockerfile

FROM ubuntu:16.04
MAINTANER Your Name "youremail@domain.tld"
RUN apt-get update -y && \
    apt-get install -y python-pip python-dev
#your Dockerfile is missing this line
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt
COPY . /app
ENTRYPOINT [ "python" ]
CMD [ "app.py" ]

docker commands to run

docker build -t flaskapp:latest .
#flask runs in default port 5000
docker run -d -p 5000:5000 flaskapp
查看更多
来,给爷笑一个
5楼-- · 2019-03-27 22:15
  1. Get the container id:

[sudo] docker ps -qf "name=<containername>"

  1. Get that container ip:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container_id>

  1. Then connect to http://<the_resulting_ip>:5000/
查看更多
男人必须洒脱
6楼-- · 2019-03-27 22:23

Maybe your docker engine is not running on the same OS where your launching your command. E.g. if you use docker with windows, the docker engine is (often) runned inside a virtual machine with a different IP, so you need to write http://[IP virtual machine]:5000/

In some configuration, the correct URL is http://192.168.99.100:5000/ (because by default, in some configurations the IP addres of that virtual machine is 192.168.99.100)

查看更多
登录 后发表回答