Tue. Mar 28th, 2023

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


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='',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



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 

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


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


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
RUN pip3 install -r requirements.txt
COPY . /app
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

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

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