Machine Learning — Deploying Model using Docker Container
By: Iftikhar Liaquat
Tags: Machine Learning, Docker Image, Ml Model Deployment, Data Engineering, Data Science

Prerequisites

Overview

In this blog, we are going to deploy the simple linear regression model (a Machine Learning Model) that we have prepared in Machine Learning — Simple Linear Regression blog.

Following are the steps that we will be going to perform in the blog:

Exposing Model’s functionality using Flask APIs Creating the Dockerfile Building & Running the Docker Image

Step-1: Exposing Model’s Functionality using Flask APIs

We’ll start with creating “app.py” module. In this module, we’ll be going to create Flask APIs to expose our model’s functionalities. Here, I’m assuming that you guys have basic understanding of Flask.

First, we are going to expose an “/SimpleLinearRegression/Train” API that will be used to train the model.

To learn more about what we are doing in the train() method kindly refer to Machine Learning — Simple Linear Regression blog.

When the “/SimpleLinearRegression/Train” API is invoked, it trains the model and save it in a file named as “linear_regression.pickle”.

Now, in order to make the predictions, we are going expose “/SimpleLinearRegression/Predict” API that will take height from the user and returns the predicted weight value.

To learn more about what we are doing in the predict () method kindly refer to Machine Learning — Simple Linear Regression blog.

What the “/SimpleLinearRegression/Predict” API does is that it loads the trained model (from the file “linear_regression.pickle” created by the “/SimpleLinearRegression/Train” API) and use it to predict the weight.

Step-2: Creating the Dockerfile

After creating the flask app, the next step is to create the docker file. This will be used to build the docker image.

I’m using python 3.7 as base image. I’m specifying my contact information for the image maintainer. Then, I’m copying the requirements.txt into the working directory. After that setting the working directory. The next couple of commands are going to update the pip and then going to install all the requirements, respectively. Finally copying the content of the directory, containing the docker file, into the working directory. The last two commands are use to run the created Flask app.

Step-3: Building & Running the Docker Image

Uptill now, we have created Flask app that is going to expose our model’s functionalities and prepared a Dockerfile . The next step is to build the docker image from the docker file. To build the docker image run the following command in the CMD.

To run above command, CMD should point to the directory where Dockerfile is present. After the command is executed, a docker image will get created.

Finally, we are going to run the image using following command:

Once the above command is executed our flask app will be available on http://localhost:5000/.

To train the model we’ll have to invoke the “/SimpleLinearRegression/Train” API as shown below:

To predict the weight for height equals to 2, invoke the “/SimpleLinearRegression/Predict” API as shown below:

Congratulations we have successfully deployed our first Machine Learning Model using docker containers.

Resources:

You can find the code that I have written in the blog from my GitHub Repository.