Deploying Machine Learning Model On The Top Of Docker Container

Niket Ranjan
Analytics Vidhya
Published in
5 min readMay 28, 2021

In this article, I’m going to create a machine learning model and deploy the model on the top of the docker container.

What is Machine Learning?

Machine learning comes under the umbrella of artificial intelligence (AI) that provides systems the ability to automatically learn and improve from experience without being explicitly programmed. Machine learning focuses on the development of computer programs that can access data and use it to learn for themselves.

What is Docker?

Docker is a set of the platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. Because all of the containers share the services of a single operating system kernel, they use fewer resources than virtual machines.

— 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 the docker container
👉 In Container you need to copy/create a machine learning model which you have created in the Jupyter notebook

Let's start, I’m going to explain the whole process step by step-

Step 1 — Here, I’m using Jupyter Notebook to create a machine learning model with a salary dataset that predicts the estimated salary.

Successfully, We have created a Salary prediction model. Our next step is to deploy this model on the top of the docker container

Step 2 — Installation of docker on the top of RHEL

  1. We are using our BaseOs — RedHat Linux-8.
  2. Go to $cd /etc/yum.repos.d and create a docker repository.

Now, we have created a docker repo.

By using $yum install docker-ce -y — nobest install the docker.

Docker Installation part is done here, Now check the docker service is running or not if it is not running start and enable the docker service.

Now, By $docker images cmd we can check the number of docker images.

By $docker ps cmd we can check the running docker os.

Now, we are going to Pull the Docker container image of CentOS image from DockerHub and create a new container.

cmd — $docker pull centos:latest

Now, We launch new os by using $docker run -i -t — name MLOPS centos:latest (i — interactive, t- terminal)

$yum install net-toolsby this, we can get ifconfig cmd on docker os.

Now, we have launched the Centos os on the top of RHEL, we can verify with both os IP.

IP Of Base Os
IP Of Docker Container OS

Now, we can check from baseOs our MLOPS docker container launched.

Step 3 — Install the Python software on the top of the docker container.

Cmd used- $yum install python3

Now, we can verify python3 is installed or not on the docker container.

After installing python3 we need to install some libraries which are required for our ML Model.

  1. $pip3 install pandas
  2. $pip3 install sklearn
  3. $pip3 install numpy

Now, we can check our library list by — $pip3 list

Step 4 — In Container you need to copy/create a machine learning model which you have created in the Jupyter notebook.

In my Base OS (RHEL-8) i.e. Docker Host I have created Machine Learning Model for Salary Prediction which is based on a Simple Linear Regression model using dataset Salary_Data.csv

Now, I’m going to create a workspace in my docker os where I’m going to copy my machine learning model.

Cmd — $mkdir task1

Now we have to copy our trained model inside our running docker container.

So we now have our model inside our docker container.

Now we have to create a python program where we load our model and execute the model on the top of the docker container.

$vi salarymodel.py (vi — cmd line editor)

Now, Our last step is to run the $python3 salarymodel.py file.

Github Link- https://github.com/NiketHub/salarypredicton_model.git

Thank You..!!

--

--

Niket Ranjan
Analytics Vidhya

I’m passionate learner diving into the concepts of computing 💻. I am a Software engineer from India skilled in Linux, DevOps, and Cloud Computing.