Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Flask app in a Docker container

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?

like image 564
srm Avatar asked Jan 19 '17 21:01

srm


2 Answers

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

like image 144
Cristian Baldi Avatar answered Sep 17 '22 14:09

Cristian Baldi


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 "[email protected]"
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
like image 32
Jobin Mathew Avatar answered Sep 17 '22 14:09

Jobin Mathew