Azure, Cloud Computing

5 Mins Read

How to Dockerize Flask Application?

An application developer can tell you that, Docker is an open-source application allowing administrators to create, manage, deploy, and duplicate applications using containers. Using docker you can make your python project portable by placing them inside a container.

Let us extend our learning about Docker and lay a foundation on containers. An earlier blog on Docker can shed more light on containers. Click here to read more. 

Now we will discuss how you can Dockerize a Flask Application. So, if you are not familiar with Flask, then Flask is nothing but a web development framework of Python. Python has two popular web development frameworks; the first one is Django and the other one is Flask. Django is quite heavier than Flask as it has many dependencies. So, we will see how you can deploy a simple Flask Application on Docker.

Prerequisites:

1. Install Python on your machine.

For installing Python on Windows, visit this URL https://www.python.org/downloads/ and download the latest Python version.

If you are using an Ubuntu machine, then use the following command.

2. Install Flask library. You can run the below command to install Flask.

Make sure that pip is also installed as well.

Guide to set up Flask Application:

Now, you need to create a python file with the name app.py inside a folder. And paste the following code in that file.

This a simple Flask Application to print some message on the browser. It will create a server and run that on port number 8080. You can choose any port as per your requirement. Now, save that file and run python app.py in the command prompt.

You will notice that a development server gets started.

Now, when you browse this URL: http://localhost:8080, the content that was written inside the return keyword will get displayed there.

It means that your Flask Application is working perfectly fine.

The next thing that you need to do is to create a file with the name requirements.txt and write a flask inside that file. This is just to install the flask library.

After that create a Dockerfile with the name Dockerfile. And make sure that you are not putting any extension at the end of the file name. And paste the below command in that.

FROM keyword is used to download the base image, which is of python and alpine3.7 is its version. Next, we are copying all the file in the current directory in the directory to my app which is inside the container. WORKDIR is used to define the working directory. If you will not use that keyword, then it will treat root as the working directory. RUN is used to run the command that we want to execute. pip install -r requirements.txt will install all the module which is written inside the file. Port 8080 will get exposed by EXPOSE command. In the code we have mentioned that the development server should run at port 8080. That’s why we need to expose that port. The last two lines are used to run the application automatically once you create a container.

So, this is about the commands which are written in Dockerfile.

The next thing that you need to do is to install Docker on your machine. Or you can launch a virtual machine and install Docker in it. The command to install Docker in an ubuntu machine is:

Make sure that Docker is properly installed on your system. You can verify that by running the below command.

If you are using a virtual machine, then you need to copy all the files in that VM. And run the following command to build the Dockerfile.

It will create an image with the name flask-image. After creating the image, you need to create a container from that image. So, you can run the below command for that.

It will launch a container, and when you browse your public IP of your VM along with the port number, then you will see that your Flask Application is running.

Conclusion:

In this guide, you have learned to create and deploy a Flask application to a Docker container. You can scale the application with the help of Docker with ease.  If you do not want to deploy directly to a virtual machine, then you can use this method to containerize the application using Docker. To attain a deep knowledge of Docker, Operating System, Virtualisation or Containerisation concepts and the use of containers for deploying & managing applications on Docker get certified through the foundation and Docker Essentials courses.

CloudThat offers extensive online and classroom training on Docker Essentials – Level 2 for beginners and Kubernetes Essentials for foundation-level courses. After completing this course, you will understand the best practices to use Docker.

Drop a comment for feedback or any queries you have relating to Docker, containers or Kubernetes and we will get in touch with you.

Stay tuned for more updates.

WRITTEN BY CloudThat

SHARE

Comments

  1. Shubham Kumar

    Apr 9, 2021

    Reply

    Great…it’s working.

  2. Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!