Deployment of Machine Learning Model Inside Docker Container
In this article we will copy a machine learning model inside a docker container and create a python program to predict the output using this model.We will create docker image for this whole setup
What is Docker Container?
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
Now that we know what a container actually does,we can start off with the hands-on part.
Note :
- I have used used RHEL 8 for this practical,you can also use CentOS or Fedora
- You can pull my docker image juzerpatan/dockerml for practicing
1.1 We will create a Simple Linear Regression model from a dataset containing the Years of Experience and Salary of 30 Employees.
The code is as follows
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression dataset=pd.read_csv('pyml_ws/Salary_Data.csv') #Reads the dataset
x=dataset['YearsExperience'] #Extracts x from the dataset
y=dataset['Salary'] #Extracts y from the dataset
x=x.values.reshape(30,1) #Converts x into 2-D arraymodel=LinearRegression() #Creates an empty model
model.fit(x,y) #Fits data into the modelexperience=float(input("Please Enter the Years of Experience: "))
pred_salary=model.predict([[experience]])
print("You can expect a salary around {} ".format(pred_salary) )
Install Docker
2.1 We have to create a yum repository for docker in the path /etc/yum.repos.d/
with the following content
[docker]
name=docker
baseurl=https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck=0
2.2 Next we have to install docker using the yum installer
yum install docker-ce --nobest -y
2.3 Start and enable the docker services
systemctl enable docker --now
Create Docker Image
For creating docker image we need to create a file named Dockerfile.In this file we will write the code for creating the image
3.1 First we need to specify the base image using the FROM keyword and then install python3 using the yum installer
FROM centos:latest
RUN yum install python3 -y
3.2 Next we need to install the required python libraries using the pip installer and create a workspace for the python program
RUN pip3 install sklearn
RUN pip3 install pandas
RUN mkdir /pyml_ws
3.3 Copy the Salary dataset and the python code for creating the ML model which we had created earlier
COPY Salary_Data.csv /pyml_ws
COPY salary_pred.py /pyml_ws
3.4 Run the python program whenever the container is launched using this image using the CMD keyword
CMD ["python3","/pyml_ws/salary_pred.py"]
The entire code for your reference
FROM centos:latest
RUN yum install python3 -y
RUN pip3 install sklearn
RUN pip3 install pandas
RUN mkdir /pyml_ws
COPY Salary_Data.csv /pyml_ws
COPY salary_pred.py /pyml_ws
CMD ["python3","/pyml_ws/salary_pred.py"]
Now,we can build the image using the docker build command
docker build -t dockerml:v1 .
Finally,we can launch the container using this image
docker run -it --name dockermlcon dockerml:v1
Finally,we have created a docker image which runs the ML model and successfully predicts the salary.
Thank You!!!