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?
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 bydocker 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, installwget
byapt-get install wget
and then test if the webserver works from the inside by doingwget http://localhost:5000
and bycat
-ting the hopefully downloaded file (ls
followed bycat file.html
, or whatever it's called).I did the following steps on my system:
flask
on the first lineIt works fine with me:
The docker process running no problem and the logs checkout fine:
When you start you docker container, check to see if it is listening port 5000. For example, on my system:
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.
Missing this part in Dockerfile
app.py
requirements.txt
Dockerfile
docker commands to run
[sudo] docker ps -qf "name=<containername>"
docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container_id>
http://<the_resulting_ip>:5000/
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)