Launching ML model on Docker

Chirayu Khandelwal
3 min readMay 27, 2021

Task Overview

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

👉 Install the Python software on the top of docker container.

👉 In Container, Create machine learning model.

Pre-requisite

  • Docker should be installed on your system.
  • Disable Firewall.

Steps for Completing the Task

  1. First we need to pull the centos image from DockerHub.

Now latest version of Centos image has been downloaded.

2. Create Container using below command

docker run -it --name ct1 centos:latest

Now, we have a new container created named as ct1 with the image centos latest version.

3. Now, we need to install python3 inside so that we can download various Machine Learning Libraries.

yum install python3-pip

4. Now, Install pandas library so that we can load the dataset.

pip3 install pandas

It has also downloaded numpy library.

5.Now, we need to download scikit-learn library which provides functions to create ML models.

pip3 install scikit-learn

After doing all the above steps our base environment is ready. Now, we need to get the dataset inside the docker container.

For this I have used WinSCP software, you can use any other methods also.

Now we have our dataset in Rhel8 but we need it in our docker. So for that run following commands.

docker cp <SOURCEFILE_PATH>  <CONTAINER_NAME>:<DESTINATION_PATH>SOURCEFILE_PATH: Path to the file inside your baseOS i.e here RHEL8CONTAINER_NAME: Path of the container name in which you want to                     transfer file. 
Note: Container should be running.
DESTINATION_PATH: Path inside docker container where you wanted to copy the file from baseOS.

Now dataset is in our Docker.

Now, its time to create a python script which can train our model and save the model in our workspace.

6. Create a file using vi.

vi ml.py

Write below code in ml.py

import 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)
output= mind.predict([[3]])
print(output)

After this press Esc then :wq to save and quit this file.

7. Now we will run this file to see our OUTPUT using python3 ml.py

Now, we have successfully created Model and done prediction inside a docker container.

That’s all, we are done !

Happy Learning !! ✌

--

--