Running a Flask application in a Docker container

I created a Docker image containing a simple Flask test application:

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 was 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 ran it using:

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

I suppose I can test the application by pointing the browser to http://localhost:5000/ , but I get a timeout. What could be wrong?

+6
source share
5 answers

Everything looks good here. Let me do some debugging.

Let's start by launching our container with the name: docker run -d -p 5000:5000 --name flask-app-test flask-app

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

First check if the Flask application is really running or maybe it crashed. Start with a simple docker ps (see, If you notice something strange there), followed by docker logs flask-app-test (these will be the Flask application logs, did it crash? Is there something strange?)

If everything looks good so far, then it's time to enter the container. Try using docker exec -ti flask-app-test bash . After you log in, install wget on apt-get install wget , and then check if the web server is working from the inside by running wget http://localhost:5000 and cat sends the securely downloaded file ( ls followed by cat file.html , or whatever it is called).

+4
source
  • Get container id:

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

  1. Get this ip container:

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

  1. Then connect to http://<the_resulting_ip>:5000/
+2
source

I took the following steps on my system:

  • I copied the code of your flash drive and the Dockerfile
  • Created a requirements.txt file with flask in the first line
  • Built an image and ran it as you did.

It works great with me:

 $ curl localhost:5000 Hello World! 

The docker process does not work, and checking the logs is 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 the docker container, check if it listens on 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 

The important thing is how you run dpcker, does it run on a Linux desktop machine? Or do you run it in a Linux virtual machine on a Mac / Windows host (through a service, such as a docker machine or other method)? If so, then the problem may be that the IP address you should be accessing is not localhost, but the IP address of the virtual machine.

+1
source

Perhaps your docker engine does not work on the same OS where you run your command. For instance. if you use docker with windows, the docker mechanism (often) starts inside the virtual machine with a different IP address, so you need to write http: // [IP virtual machine]: 5000 /

In some configuration, the correct URL is http://192.168.99.100/10000/ (since the default IP address of this virtual machine is 192.168.99.100 in some configurations)

+1
source

This part is missing in the 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 
0
source

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


All Articles