DEPLOY ML MODEL ON DOCKER

Harshal Kondhalkar
4 min readMay 31, 2021

--

ML and Docker Integration

TASK 1 đź’»

TASK DESCRIPTION:

👉Pull the Docker container image of CentOS from DockerHub and create a new container

👉 Install the Python software on the top of docker container

👉 In Container you need to copy/create machine learning model which you have created in jupyter notebook

👉 Create a blog/article/video step by step you have done in completing this task. Submit the link of blog/article or video

  • pre-requisite: Docker installed on your OS

To run a OS on top of the Docker, you first need the OS image

>To download the OS image, visit the site hub.docker.com and search centos in the search engine there

Copy the docker centos pull command

To start the docker services in the Linux, use command:

systemctl start docker

To run CentOS image in the Docker, paste the copied image on the terminal

docker pull centos

Image of the centos will be downloaded in the docker

Verify if the image has been download, using command:

docker images

Masquerading allows the docker ingress and egress

Specifically allow incoming traffic on port 80/443

firewall-cmd — zone=public — add-port=80/tcp

firewall-cmd — zone=public — add-port=443/tcp

Reload the firewall to apply the permanent rules

firewall-cmd — reload

Restart the docker as shown

systemctl restart docker

To run the centos container use command:

docker run -t -i centos:latest

  • where -t= enables terminal of centos
  • -i= makes the terminal interactive

Make any folder in your container

mkdir “name of the folder”

Now, in a different tab/terminal, copy the Data-set you have (“SalaryData.csv” in this case) from the /root folder into the container, in the folder you have created before

  • Mention the container name (“stupefied_knuth” in this case)

Command to check name of the container

docker ps

Come back to container terminal

Now we have to install some software and libraries in the conatiner, starting with python

Install python software in the conatiner

yum install python3

Wherever asked y/n, press y

After downloading python software, now we need to download libraries like pandas and scikit-learn

  • Pandas is used to read the .csv file
  • scikit-learn library is used for Linear Regression Model

Install pandas library

pip3 install pandas

Install scikit-learn library

pip3 install scikit-learn

Start python software

python3

Start creating your ML model in the python

import pandas
db=pandas.read_csv(“Salary_Data.csv”)
print(“Dataset Loaded Successfully”)

# Assigning feature and target
# x = feature / independent variable
# y = target / dependent variable

x = db[“YearsExperience”]
y = db[“Salary”]

# Loading LinearRegression
from sklearn.linear_model import LinearRegression as LR()
model = LR()

#Converting pandas data into array using “values” function and reshaping it into 2D array

x = db[“YearsExperience”].values.reshape(30,1)
model.fit(x,y)

#Predicting salary, based on given data set and pattern/model created by the fit()

model.predict([[1.1]])

# Saving Trained Model
import joblib
joblib.dump(model,”salary.pk1")
print(“ Model Saved Successfully”)

Thus the task is completed.âś…

Thank-you!

--

--

No responses yet