Machine Learning Model On Docker Container

Ranga Mani kumar
4 min readMay 27, 2021

Running the Machine Learning model on Docker Container

Introduction :

Machine Learning :

Machine Learning, a branch of Artificial Intelligence, is a method to make our machine to learn historical data for predicting. We have to make our machine intelligent to predict. But how can it predict ? We need to collect the historical data by research and using Data science. After collecting using different techniques and libraries we can create a model that it can predict. This is called Machine Learning.

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.

So it is very easy to run our model in container at any time because we have all depedencies of model at anywhere. We not need to worry about that. We also not need to create our model again and again.

Task :

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 you need to copy/create machine learning model which you have created in jupyter notebook

Lets do step by step

Step-1 : First, we need to start the docker service in our base OS

systemctl start docker

You might face SE Linux issue but for temporary and fast solution, we can run this command. This will disable SE Linux until you reboot

setenforce 0

After running that restart your docker service

systemctl restart docker

Step-2 : Now, pull the centos image from docker hub using this command

docker pull centos:latest

Step-3 : After the image is pulled, run a container using centos image using this commad. Here Iam giving my container name as test.

docker run -it centos:latest --name test 

Step-4 : Our conatiner will be launched and running, now we have to install python3 as our machine learning model is written is python as a language.

yum install python3

Step-5 : Now, we have to install required libraries for our machine learning model to run. We can install them using pip3 as it also been installed with python3.

pip3 install sklearn scikit-learn==0.23.1 joblib

Step-6 : Now we have to run our model. But where is the model ?

You can do this in 2 ways. One is you can just create or copy your code in the container or using github to clone it.

Another way is after running your model succesfully, we can save that model for our future use.

At wherever you are use joblib library to save your model, after saving it. it creates .pkl file in which our machine learning will be saved. Now we just need to copy that .pkl file to our conatiner.

import joblib  
joblib.dump(model, 'salary_predictor.pkl')

In this we have to pass two arguments 1) model name 2) Name for the .pkl file

After that we have to load our model using joblib library and we can create a python script in a way to load our model and we enter the input values and automatically predicted output print on screen like an application. We also copy this python app file to our container.

import joblib
model = joblib.load(‘salary_predictor.pkl’)
p = input(“Enter the experience of employe: “)
output = model.predict([[int(p)]])
print(output)

Now, we just run our python app file in our container and it prompts for input and as soon as we enter, we have the predicted output on our screen.

We can also create an image using this container

docker commit test484 rangamani54/ml_image:v1

Here test484 is conatiner name and rangamani54 is repository of docker hub and ml_image is name with version as v1.

Here is my demo video on this

Part-1 Demo

Part-2 Demo

Github link

Thankyou…

Keep learning…

Keep sharing…

--

--