How to run ML model on Docker Container

Bhanudas Rane
4 min readMay 27, 2021

Hello Learners,

Welcome to my article on TASK-01 given by LinuxWorld Informatics Pvt Ltd.

Task Description :-

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.

Docker :- Docker was released in march 2013 and till today it is used in agile based projects. Docker is a container management system. Main purpose of docker is develop, ship and deploy anywhere .

Machine Learning :- Machine learning is a concept by using we can get predictions from machine. In machine learning ,machines learn by past experience and create some pattern/mathematical formula to get output. Human stores the information in the form of pattern.

So , in this article we see how to run machine learning model on docker container. For that this are following steps:-

Step 1:- Install Docker in System.

First we check that docker is install in our system or not . So that ,use this command →

#docker info →→give informaion about docker.

If you are not having docker then use this command to install docker →

#yum install docker →→use to install docker in system.

Step 2:- Check any os/container running in docker?

If we have install docker in system then check how many os running in system. Use this command →

#docker ps →→check running os in docker.

Step 3 :- check image of os?

Check whether you are having image of os or not , for this use command →

#docker images →→ show how many images we have.

If you are not having os images then go to “hub.docker.com” and select os/container that you want.

Step 4:- Pulling the OS/Container in docker.

After selecting the operating system image from dockerhub , install it in docker. I install centos os. To pull this use this command →

#docker pull centos:8 →→ use to pull OS in docker.

Step 5:- Run the OS/Container .

Run the operating system in docker use this command→

#docker run -it centos:8→→Use to run OS/Container in docker.

After colon give the version of OS/Container that you want ,or you can use :latest to run latest version.

Step 6:- Install python in Container.

Install python3 in operating system that use to run ML model. For this use command→

#yum install python3→→Use to install python.

Step 7:- Install libraries (Pandas, Numpy, scikit-learn)

Install libraries that help to build model . We will install pandas, numpy and scikit-learn libraries. For this use following three command→
#pip3 install pandas → →Use to install pandas library.

#pip3 install numpy → →Use to install numpy library.

#pip3 install scikit-learn → →Use to install scikit-learn library.

Step 8:- Install git to clone git repository.

We will having multiple ways to copy our model in docker images .Here we use git for this. To clone git repository first we have to install it . For this use command→

#yum install git → → Use to install git in linux

Step 9:- Clone the git repository.

Clone the repository so we can get ML model file. You can click on download to get repository. You can use this command→

#git clone repository_name → →Use to clone repository.

Step 10:- Copy model file to root directory.

Copy .pk file in root directory, for this simply use cp command→

#cp source_address destination_address → →Copy the file.

Step 11:- Create python file to run model.

Create .py file and run following code on them . It will help you to run model

#vi salary.py → → use to open and create file on terminal.

Step 12:- Run python file that you created.

Finally run the file & your ML model ready for prediction.

#python3 salary.py → →Use to run python file.

Finally we done our Task ,and successfully run ML model on Docker ……👍

Thank You !!

-Bhanudas Rane.

--

--