Deploy a flask app on Docker container

Urvishtalaviya
4 min readMar 19, 2021

Hey There!!!

In this article, I will show you how to deploy your Flask application on the docker container. It would fun to do it. right? So, let’s get started without wasting much time. I will use RHEL for development, So all the commands will be according to it.

What is Flask?

Flask is a micro web framework build in python used for developing web applications. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. We can create RESTful APIs with Flask as well. Flask is much easier to learn.

What is Docker?

Docker is a set of the platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. Because all of the containers share the services of a single operating system kernel, they use fewer resources than virtual machines.

I won't go deeper inside the Flask because I assume that you already know how to write code in Flask. I already created a web application with simple HTML and CSS. The application is predicting whether the passager will survive or not on the titanic with the KNN machine learning algorithm.

Note: I had created this model when I was learning Machine Learning, So please ignore the feature engineering, just focus on the integration part.😜

Docker image

Docker images are the same as iso files for Operating System, it helps to create a container for our applications. They are smaller in the size, because of this it helps to launch the container faster. Docker has a centralized repository for storing these images and the repository is https://hub.docker.com/, We can create our own images and upload them on the repository too.

We have two ways to create docker images.

Commit

First, launch a container and install all of your requirements. Then save the container with the commit command.

docker commit [CONTAINER_ID] [new_image_name]

Dockerfile

As you can see from the name that we just have to create a file that contains steps to create an image. And save that file with Dockerfile name only. The below-given command will build the image for us.

docker build -t <iamge-name> .

In this article, We are going to use Dockerfile as a way to create an image. It has some keywords that let me explain to you first.

  1. RUN: RUN keyword runs the command whatever you to it at build time.
  2. WORKDIR: It will change the current work directory.
  3. COPY: It will copy files from your local system to the image.
  4. ENTRYPOINT: It will execute the command whatever we give to it at run time.
  5. CMD: CMD also runs the command at run time but we can override the command while ENTRYPOINT doesn’t.
Dockerfile

I wrote the above Dockerfile for my application. Let me explain to you what it will do.

The first-line FROM keyword will take the image for a base to deploy the application on. Second-line will install python36. Third-line mkdir directory for copying the code. The fourth will change the working directory where we paste the code. The fifth will update the pip and the sixth install all the libraries which are mentioned in the requirements.txt file. The last line will run at the time of container launching. RUN keyword runs at build time and CMD keyword runs the command at run time.

Then build with the Dockerfile with the command given above.

Docker container

After building the image, we’ll launch a container out of it for testing the app but also have to expose it for outside world connectivity and this concept called patting.

docker run -it -p <host_port>:<container_port> <image_name>

In this application, I have used 5000 port number of docker and we’ll pat it with 5000. You can use any of the port number which is free.

web application

If you wanted to download the image then just use docker pull urvish667/machine-learning:v1 command. Here is a link to the image

Thanks for reading. Hope you find it informative.

--

--

Urvishtalaviya

Competitive Programmer | Machine Learning Enthusiastic | Bigdata Enthusiastic