🧠Deploy Machine Learning Model inside Docker Container🐳

rishabhsharma
3 min readMay 27, 2021

--

In this article we will be looking how we can deploy and run Machine Learning models inside docker containers.

Here I am working with RHEL-8 and I already have docker inside my RHEL-8.

To know more how we can install and configure docker inside RHEL-8. Refer to https://hrishabhsharma183.medium.com/configuring-httpd-webserver-setting-up-python-interpreter-and-running-python-code-on-docker-4d01dd501b4

First we have to start the docker service

# systemctl start docker
# systemctl enable docker
# systemctl status docker

After starting docker service we have to pull the image from docker hub which we require to launch container.

Here we will be using centos image.

Command to pull image

# docker pull centos:latest

To check the images command is

# docker images

Command to launch container

# docker run -it --name ml centos:latest 

within a sec we have our container launched. After running the above command we will land inside our container. To manage our code properly we are creating separate workspace/directory. We will keep our code here.

# mkdir MLOps# cd MLOps

In this image yum is already configured, so we can use it to install software/packages.

Now we have to install python3

# yum install python3 -y

After installing python3 we need to install some libraries which are required for our ML Model.

# pip3 install pandas
# pip3 install scikit-learn
# pip3 list

In my Base OS (RHEL-8) i.e. Docker Host I have created Machine Learning model for Salary Prediction which is based on Simple Linear Regression model using dataset Salary_Data.csv

# SalaryPrediction.pyimport pandas as pd
dataset = pd.read_csv("Salary_Data.csv")
X = dataset['YearsExperience'].values.reshape(30,1)
y = dataset['Salary']
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X,y)
ye = input("Enter Years of Experience: ")
salary = model.predict([[float(ye)]])
print("Salary :",salary)

Now we have to copy our trained model and dataset inside our running docker container named ml to our created workspace named MLOps.

To do this we have command

# docker cp SalaryPrediction.py ml:/MLOps
# docker cp Salary_Data.csv ml:/MLOps

So we now have our dataset and model inside our docker container.

Running trained model to predict the salary based on Year of Experience:

# python3 SalaryPrediction.py

Done!

⚡Keep Learning Keep Growing!

--

--

rishabhsharma

AWS Certified ☁️ | PySpark | DevOps | Machine Learning 🧠 | Kubernetes ☸️ | SQL 🛢