Deploying Machine Learning Model on Docker

In this blog, we are going to deploy a Machine Learning Model on the top of Docker container. For this, we need various software as a prerequisite. So let's start this by installing Docker.

To install Docker first we need to configure yum for docker. So paste the below code in your repo dir cd /etc/yum.repos.d

yum install docker-ce --nobest
systemctl enable docker --now

These commands will install and start the container services.

After that, we need to launch a docker container on which we will run the Machine Learning model.

To run a container we can run the following command

docker run -it --name ml centos:latest

After running the docker run command we will directly land into the docker container. Now, we need to install some packages like CLI text editor- vim, and python3.

yum install vim python3-pip -y

After installing python3 we need to install some necessary py libraries to train our Machine Learning Model.

pip3 install numpy pandas sklearn joblib

After installing all the required libraries we need to copy the dataset which we are going to use to train our model.

docker cp SalaryData.csv os1:/

Now, we can write our Machine Learning code in our docker container using vim as a text editor.

vim ml.pyimport pandas 
ds= pandas.read_csv('SalaryData.csv')
from sklearn.linear_model import LinearRegression
mind= LinearRegression()
x= ds['YearsExperience'].values.reshape(30, 1)
y= ds['Salary'] mind.fit(x,y)
mind.fit(x,y)
import joblib
joblib.dump(mind, 'model.pk1')

This line shows that our code is a success and our model is created( model.pk1).

We need another program to run the pre-created model. Hence, I have created another program for running the model.

vim final.pyimport joblib
mind= joblib.load('model.pk1')
x= input("Enter the experiance in years:")
x=[[x]]
op =mind.predict(x)
print(op)

This code will first load our pre-trained model(model.pk1) and ask the number of years of experience then it will predict the estimation of the salary based on the years of experiance.

Output