“How to Run ML Model on Docker Container…”

Kaustubh Chaudhari
3 min readMay 31, 2021

Hello Guys,

Welcome you to this new Article where we will know about launching an ML model inside Docker Container. It sounds fun right….

So, Let’s get Started …

Firstly we will start with Introduction to Docker for those who are not familiar with Docker…

# What is Docker ?

Docker is a Container Engine which is capable of Launching containers. Basically containers are small and tiny OS which you can launch within seconds. It is not a small thing to make environment ready within seconds. It saves a lot of deployment time and helps in reducing delivery time of Software. Many companies are shifting themselves to Containers for faster development and easy deployment of Software.

Docker Logo

How to install Docker inside Linux System :=

Step 1 : Setting up yum repository for Docker :

  • Check whether your system has Docker or not :

“docker info”

  • Check for Docker Package :

“rpm -q docker-ce”

  • If not present then go to Yum directory and create a file for docker repo :

“cd /etc/yum/repos.d/”

“vim docker.repo”

  • Paste this content inside that file :
  • Install Docker using yum :

“yum install docker-ce — nobest”

  • Start and Enable Docker :

“systemctl start docker-ce”

“systemctl status docker-ce”

“systemctl enable docker-ce”

Now You are Good to go …

Creating Image for ML :

  • Install Python, Pandas, scikit-learn, joblib…

“yum install python36 -y”

“pip3 intall <package name>”

Now we will have to get one Dataset . I am using Salary dataset(“salary.csv”).

ML Code :

import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression dataset=pd.read_csv('Salary_Data.csv') #Reads the dataset
EXP=dataset['YearsExperience']
Y =dataset['Salary']
Exp=Exp.values.reshape(30,1)
Mind = LinearRegression()
Mind.fit(Exp,Y)
Experience = float(input("Enter your Years of Experience: "))
Sal_pred=model.predict([[Experience]])
print("Expected Salary : {} ".format(Sal_pred) )
  • Save this code in “ML.py”
  • Create Docker file in same Directory

“vim Dockerfile”

  • Append these Contents :
FROM centos:latest
RUN yum install python3 -y
RUN pip3 install sklearn
RUN pip3 install pandas
RUN mkdir /Docker_ML
COPY Salary_Data.csv /Docker_ML
COPY ML.py /Docker_ML
CMD ["python3","/Docker_ML/ML.py"]
  • Now, Build an Image :

“docker build -t docker_ml : v1 .”

  • Now Run Container with newly created image :

“docker run -it — name ML_in_Docker docker_ml : v1”

  • Now the Container is Successfully Launched and You can give input as Year of Experience and You will get Output as Expected Salary.

Here Our moto was not to Create some ML code But to see How to Deploy it Over Container.

Hope You will Like it …

Stay tuned for New Content…

Thank you !!!

--

--