DEV Community

Pierangelo
Pierangelo

Posted on

Dockerize a Flask App

How dockerize a Flask app…

here my very basic app:
main.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")
Enter fullscreen mode Exit fullscreen mode

N.B: above I have set public ip 0.0.0.0, beacuse with 127.0.0.1 it’s reachable only inside the container and not from the outside outside.
create a file requirements.txt
and insert all the pip libraries that your app require (in my case only Flask).

requirements.txt

flask
Enter fullscreen mode Exit fullscreen mode

create a file named Dockerfile:

FROM ubuntu:18.04
LABEL maintainer “yor name <your@email.com>”
RUN apt-get update -y && \
apt-get install -y python3-pip python3-dev
# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip3 install -r requirements.txt
COPY . /app
ENTRYPOINT [ “python3” ]
CMD [ “main.py” ]
Enter fullscreen mode Exit fullscreen mode

build the image launching from terminal the command:
docker build -t name_of_yuor_image:latest .

run the container launching from terminal the command:

docker run --name name_your_container -p 5000:5000 -d name_of_yuor_image:latest

connected to address: http://0.0.0.0:5000

Watch the video tutorial:

https://youtu.be/Pjd4k4l8oV0

Top comments (1)

Collapse
 
gravesli profile image
gravesli

I think you are great! I built a state monitor using Flask!
Flask State Github:github.com/yoobool/flask-state .
Should i can get some improvement suggestions from anyone? Thanks~