Deploying Machine Learning Model Inside a Docker Container

Varun
4 min readMay 30, 2021

--

Hello Readers !!

Today I will guide you on How to implement an ML (simple linear regression) inside a docker container.

So, Let's get started :

Before implementing the Machine Learning model in your container. there is a pre-requisite:

  1. Docker should be installed on your base OS.

// Here I used RHEL8 as my base OS.

You can follow this URL to install docker in your base OS (I Considered RHEL8 as the base OS)

https://linuxconfig.org/how-to-install-docker-in-rhel-8

After installing Docker, I want to set up CentOS as my container. for this, first, we have to pull the docker container image of CentOS from the docker hub website.

pulling docker CentOS image:

docker pull centos:latest

here, latest represents version

Creating a Container:

docker run — name ml -it centos:latest

here, ml is name of container. you can give any name of your choice.

-it represents an interactive terminal offered after launching the container.

As, you can see in the above image, after launching a new container, we are automatically logged in to that container.

as in this newly launched container, python3 is not installed by default.

So, we have to install it through the yum program.

Now, for creating an ML program, we need to install some python libraries like pandas, numpy, sklearn, joblib, etc.

for installing such libraries, we use pip program provided by python3.

pip3 install pandas

pip3 install sklearn

After installing these libraries, we import modules from them and create a simple linear regression program using python language.

In this program, we provide some dataset to our machine, and using linear regression algorithm, we will create an experience, better known as a model, and using that model we will perform the analytics i.e will give the intelligence to our machine so that it can predict future outcomes just like human beings.

copying dataset from base OS to Docker Container:

Simple Linear Regression Program code:

Here,

used Linear Regression function to create empty mind in memory.

using fit function to train our machine so that it observes dataset and creates a model.

after creating model in mind, using joblib module, we stored it in a file so that no need is there to create/train that mind again.

using joblib module, we can load the mind from that file back in the memory.

Final python program file provided to end-user:

While executing this program file, users have to enter their years of experience and in return, the machine will use its intelligence to predict their salary.

THANK YOU FOR READING !!

HOPE YOU LIKE IT !!

--

--

No responses yet