In this post, we are going to learn how to create and deploy a basic flask application on docker? To understand this process we will go through different steps

First, we will create a basic flask application, then we will define the requirements into the requirements.txt file. Then we will create the docker file build the container. Finally, we will utilize the particular docker container to run our flask application on our local system. So let's get started!

Before starting this process docker must have been installed on your system. Now just open the code editor, you can use the editor of your choice. Open project directory of your project. First, create a new file named file inside the project directory. First of all import the packages like flask to create the flask application, render_template to render our HTML template, abort to request with and HTTP error code early, url_for to create and prevent the overhead having to change URLs, JSON to read the string data, and JSONIFY to serialize the data into the JSON format.

from flask import (Flask, render_template, abort, url_for, JSON, jsonify)

Then create the instance of the Flask class and save it to the app variable, it actually takes two arguments first one is the name of the current module which is passed as the dender name, the second argument is optional which is template_folder mentioned current directory of this project.

app=Flask(__name__, template_folder='.')

Now we will open the JSON File with the read permission and read the data into the data variable name.

with open('file.json', 'r') as f:

Next, we have defined the route with the index function, and inside this function, we are simply rendering a template by using the render_template method, and we are passing the JSON data just read above and read the data into data variable to that particular template.

@app.route("/", methods=['POST',GET])
def index():
    return render_template('index.html', title="page", jsonfile=json.dumps(data))

Next, we have defined the main function which is the main driver function, and the run method of the flask class run the application on the local server with your mentioned host.

if __name__ == '__main__':, host='')

Now let me create the JSON file put some data inside that: For each, we have the name, age, and the city.

{ "name":"Ali", "age":28, "city":"Islamabad"}

Now we will create the requirements.txt file within the current activated environment we simply run the command as

pip freeze >requirements.txt

It will pull down all of the requirements into the requirements.txt file


Our application is ready to deploy. Now let start the process of containerization. So first of all inside the root directory create another file named Dockerfile. You must have the docker file name correctly, there are no extensions of the docker file, and it's case sensitive.

The docker file contains the Ubuntu instance

FROM ubuntu:18.04

And also we utilize the run command to update all of the dependencies and install the python-pip.

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

Then we have defined the working directory of this project


Copy all the files from the host machine to the Docker server.

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

After that, we have installed all of the dependencies mentioned in the requirements into the requirements.txt file.

RUN pip install -r requirements.txt

Finally, we have defined the command to run our flask application. Save that file and open the terminal. inside the terminal, you simply have login into the project directory of this project.

Now first have built the image. But before running this command make sure that docker must have been run on our system then type the command as:

docker build -p my_flask .

which is my_flask is image name and dot represented to the working directory of this project. After completing this process of creating the image run the command as

docker run -p 5000:5000 my_flask

We launched the container successfully But checking the image is creating or not to run the command as:

docker ps -a

Open the browser and type the

So check now successfully running the container image on the browser. If you have any problem with this process then comment below. Thanks

Feb. 19, 2022
Web Development

More from 

Web Development


View All