Dockerize a Flask Web Application

Dockerize a Flask Web Application

In my previous post, I have shown you how to build a basic Flask application, now lets dockerize the application.

 

You can follow the Django 3.0 application which we will use here from the below link. 

 

How to build a basic Flask Application

 

Here’s the myflaskapp repository on Github

https://github.com/amarnath091/myflaskapp.git

 

Assuming that I will place this application on my desktop folder, I will move to the desktop directory and then create a folder for this application.

mkdir myflaskapp

Now move this directory,

cd myflaskapp

Now lets create a file called “flaskapp.py” in the myflaskapp directory and then type this basic code to run the application

from flask import Flask           # importing flask
flaskapp = Flask(__name__)             # creating flaskapp instance
@flaskapp.route("/")                   # defining end point /
def hello_world():                      # calling method hello_world
    return "Hello World! Welcome to my Flask app. Let's dockerize! # returns content in " "
if __name__ == "__main__":        # for running python flaskapp.py
    flaskapp.run(host='0.0.0.0',port=5000)   # run the flask app / you can also specify desired port number here


To dockerize any application, we need to create a requirements.txt file which will be mentioned in Dockerfile to install all dependencies in a single shot.

To install all your dependencies you need to create a “requirements.txt” file and you can either just create this file and mention Flask in it.

In requirements.txt
Flask==1.1.1

(or)

If you have more dependencies then you can run this command to dump all the dependencies into requirements.txt file. Make sure you are in the virtual environment and then enter

Pip freeze > requirements.txt

(flask_env) RSAs-Mac:myflaskapp rsa$ pip freeze > requirements.txt

(flask_env) RSAs-Mac:myflaskapp rsa$ cat requirements.txt 

click==7.1.1

Flask==1.1.1

itsdangerous==1.1.0

Jinja2==2.11.1

MarkupSafe==1.1.1

Werkzeug==1.0.1

Now all the dependencies will be in the requirements.txt file.

Create Dockerfile:

Create a “Dockerfile” with no extension to the file name in the same myflaskapp application directory.

Dockerfile is the heart of Docker as we can mention all that we need in this file and when you build and run, it goes through this file and installs in sequence as mentioned in the file. It fails if something is not matching up properly. Mentioning the port numbers is where everyone makes a mistake so pay proper attention while defining the port numbers.

Let’s start writing the Dockerfile now:

Taking the image of ubuntu 18.04 as the OS on which we will be hosting our Flask application. You can find other images from Docker Hub

FROM ubuntu:18.04

Now we are installing and updating pip & python3 in the image

RUN apt-get update -y && \
    apt-get install -y python3-pip python3-dev

Now copy the requirements.txt file which consists of the dependencies

COPY ./requirements.txt /app/requirements.txt

Let’s change current directory to /app in the image as we will be placing our app files

WORKDIR /app

By this command we are installing all dependencies mentioned in requirements.txt

RUN pip3 install -r requirements.txt

This command copies all files from current local directory to /app in ubuntu

COPY . /app

Exposing port number 5000

EXPOSE 5000

Mentioning python3 in ENTRYPOINT once will not require you to enter multiple times for multiple commands in the next step

ENTRYPOINT [ "python3" ]

Running app now same as "python3 flaskapp.py" with ENTRYPOINT and CMD

CMD [ "flaskapp.py" ]


Your Dockerfile should look like this:

FROM ubuntu:18.04
RUN apt-get update -y && \
    apt-get install -y python3-pip python3-dev
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip3 install -r requirements.txt
COPY . /app
EXPOSE 5000
ENTRYPOINT [ "python3" ]
CMD [ "flaskapp.py" ]

Once all of them are saved, you are now ready for building the image and running the container.
From the myflaskapp directory in terminal, enter

docker build -t myflaskapp .

You have now built the myflaskapp. To check your image, you can enter the command

Docker images

RSAs-Mac:myflaskapp rsa$ docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
myflaskapp                           latest              59e53d444f8f        17 minutes ago      496MB

Now lets run the container at port number 5000 in detached mode.

docker run -d -p 5000:5000 myflaskapp

That’s it! Now go to your browser and check your localhost and you should see the app running.
http://localhost:5000/

 

Let’s push this to Docker Hub repository so it will be available to anyone and anywhere depending on your requirements.

On you terminal, enter
docker login
docker image build -t <DOCKERHUB_USERNAME>/myflaskapp:1.0 .
docker push <DOCKERHUB_USERNAME>/myflaskapp:1.0

This should now be pushed into your Docker Hub repository.
You can access this image from anywhere just by pulling it by using the below commands

docker pull <DOCKERHUB_USERNAME>/myflaskapp:1.0

And you can run it simply by using this command.

docker run -d -p 5002:5000 <DOCKERHUB_USERNAME>/myflaskapp:1.0

Well, that’s a way of dockerizing a basic flask application.

Please leave your comments on this post and feel free to drop any suggestions on what you are looking for and I will try to post on them in the future articles.

 

Recent Updates

Recent updates on DevOpsPal

Social

Follow us of social for latest updates.

DevOps Hub

Links to get started.