12

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?

Sajjan Singh
  • 2,523
  • 2
  • 27
  • 34
srm
  • 548
  • 1
  • 4
  • 19

5 Answers5

12

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).

Cristian Baldi
  • 401
  • 5
  • 9
  • `docker logs flask-app-test` shows `* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger pin code: 236-035-556`, but `$ docker exec -ti bash flask-app-test` shows `Error response from daemon: No such container: bash` – srm Jan 19 '17 at 22:23
  • @srm my bad, the right `docker-exec` command was `docker exec -ti flask-app-test bash`. Wil now edit the answer. – Cristian Baldi Jan 19 '17 at 22:25
  • Ah OK, it returns the HTML response: `Hello World!`, so it is working inside the container. I understand the container has no GUI, so there's no way of using a browser to test the app? – srm Jan 19 '17 at 22:27
  • Well, we should have exposed/mapped the port 5000 to the host, so pointing your local browser to http://0.0.0.0:5000 should work. But I am not understanding why it isn't. – Cristian Baldi Jan 19 '17 at 22:30
  • 1
    From outside the container I tried `curl http://0.0.0.0:5000/` which gives me `curl: (7) Failed to connect to 0.0.0.0 port 5000: Connection refused` – srm Jan 19 '17 at 22:51
  • @srm I am facing same error, did you find solution? if yes can you please post solution? – M005 May 03 '20 at 14:58
3
  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/
lee-pai-long
  • 2,147
  • 17
  • 18
  • 1
    As I replied to @cristian-baldi, I've got it working. – srm Jan 22 '17 at 19:39
  • Thank you for your answer, but when I run the command `sudo docker inspect -- format ....` I get nothing. – Catalina Chircu Dec 19 '20 at 07:30
  • @CatalinaChircu if you got nothing it should mean you container doesn't exist anymore, do you see it when you do a `docker ps -a` ? – lee-pai-long Dec 22 '20 at 10:43
  • @lee-pai-long : Thank you for your answer. I solved my problem in fact, and I got everything good when running `docker ps`. My problem was that I had to change `CMD=["app.py"]` to `CMD ["flask", "run", "--host", "0.0.0.0"]`, and without any `ENTRYPOINT`. I also added the instruction `--interactive` when running. – Catalina Chircu Dec 22 '20 at 12:39
  • 1
    @CatalinaChircu I'm happy youp solve you problem. I wish you well. – lee-pai-long Dec 23 '20 at 18:33
2

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.

Angelos
  • 1,632
  • 16
  • 25
  • I'm using a Mac and the Docker container runs via Docker for Mac. As I replied to @cristian-baldi, I've got it working. – srm Jan 22 '17 at 19:39
1

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)

1

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