Deploy Machine Learning Model Inside Docker Container

Laveena Jethani
5 min readMay 27, 2021
Deploying Machine Learning Model Inside Docker Container

Docker is a Containerization tool. Docker can launch OS within 3 sec called Container. If we compare docker with virtualization we launch a virtual machine in 15–20 min. But docker provides the power to launch OS within seconds.

Machine Learning model is trained from the historical datasets. Helps to predict the values.

Machine learning Engineers build the models. But Where deploy the model. For deploying the ML model environment is needed. Here term environment meaning OS with an installed library of machine learning.

We can deploy a machine learning model on :

  • Bare Metal
  • Virtual Machine(maybe on cloud or local)
  • Docker Container

We choose Docker for deploying the machine learning model:

There are many features we can get if we deploy our model to docker container Because:

  • The docker container is the fast install
  • If the container crashes we can launch another container in the same environment
  • For launching the container we don’t need many compute units.
  • Docker container helps to save time

If I want to deploy the machine learning model on top of container :

  • The container provides OS to us.
  • But for running machine learning models we need python installed and some machine learning libraries.

For making the environment for deploying machine learning model on top of docker container we need to follow these steps:

For doing this practical docker should be configured.

Step: 1

Pull docker centos:7 image

pulling docker image

Step: 2

Launch Docker Container with centos:7 image

launching docker container

Step: 3

Inside docker container install python.

installing python

Step: 4

Install machine learning libraries:

pandas

pandas is a software library written for the Python programming language for data manipulation and analysis.

NumPy

NumPy is a python library adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

we don’t have to install NumPy it already installed with pandas library.

Scikit-learn

Scikit-learn is a free software machine learning library for the Python programming language.

joblib

joblib is a python library used for storing and importing machine models into files.

joblib library already installed with scikit-learn library.

For going out from container without stop press (ctrl+p+q).Now we are at the docker host.

Step: 5

From the docker host copy the dataset. To copy the dataset from the docker host to the docker container. We will use the [ docker cp ] command.

Reference: https://docs.docker.com/engine/reference/commandline/cp/

This two are available at the docker host we will copy these two files.

Checking status of container:

Let’s copy the files one by one

Copying the Salary_Data.csv dataset file
Copying LinearRegression.py file

Let’s go to container by command : [docker attach <container_name>]

Let’s check what file is available at the docker container (check status of data is copied or not):

both files are copied at the docker container

Step: 6

Make code of python for machine learning model. In the code, we use joblib library for dumping the machine learning model inside a file.

Here I am taking the example of Linear Regression and using a dataset of How the salary of the employee depends on the year of experience.

Salary_Data.csv Dataset

Dataset used in ML

ML code for Linear Regression

LinearRegression.py

Let’s run the code LinearRegression.py

Output

After running the LinearRegression.py code we have got bias(b) and weight(w) values.

After running this one model file(model_salary_predict.pk1) is saved at the folder:

Step: 7

Load machine learning model file(model_salary_predict.pk1) for predicting the values.

predict_salary.py

predict_salary.py code

Output of predict_salary.py

I have successfully prepared the environment for running the machine learning model inside a docker container.

I have also created a machine learning code for salary prediction from the year of experience of an employee.

Here I have a very simple example for understanding the concept of how we can deploy our machine learning model inside a docker container.

We can take any model of machine learning for deploying the model inside a docker container.

This is how we can use docker tool power to launch OS within seconds. And our environment ready for running any program.

I hope this article you like!!!!!

Happy Learning 😄😄!!!

Thank You!!!!!

--

--

Laveena Jethani

Technical Blog Writer | Research & Review different technologies | ARTH learner