Docker Container Integration with Machine Learning Model in Python

Docker Container Integration with Machine Learning Model in Python

What Is Docker?

Docker is a set of 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.

How to setup Docker on your BaseOs?

In-order to to install Docker in the OS(in my case, REDHAT), we need to create a docker repo file in /etc/yum.repos.d directory.

vim /etc/yum.repos.d/docker.repo

Add the following content to the repo file:

5 add url to repo.png

Run This:

yum repolist

So we have configured Docker repo and now we can install docker by using command:-

yum install docker-ce --nobest

Here, ce in the last stands for Community Edition which is free to use.

It will take some time to download and install the docker, if downloading fails try again after some time. Docker is now installed in your system. You can check status of your docker like this:

10 Docker start status.png

Now Docker container uses Docker images, we can download docker image like this (in my case i have preinstalled image, but it will download the centos image if you run the command for first time):

docker pull centos:8

11 pull centos image on docker container.png

Now we need to run this image like this:

docker run -it --name MLos centos:8

This will direct you to the terminal of centos installed on docker container.

12 run centos image and gime name to os.png

Hence your we were successful Docker container image of CentOS image from DockerHub and create a new container, we can use ls command to list the items in centos installed.

How to Install Python Software on Top of Docker Container?

Now you can install the python3 on your container with simple linux commands. yum install python3

13 Install python3 on container.png

We can install few libraries that we will be needing in future for Machine Learning Model.

pip3 install scikit-learn

pip3 install joblib

pip3 install pandas

Hence we were succesful in Install the Python software on the top of docker container

How to train the Model?

After Installing the Libraries, we need to paste the model in our Container. For creating a Model we need a dataset. We in this, after we train the model are going to predict the salary of employees based on their experience in years. So we will first download a dataset in our BaseOS (do not close the docker container, if closed you can start the docker container as by following command: docker attach MLos).

You can download the same dataset from here. Now Open the terminal in the folder containing the dataset as follows:

18 Open Terminal.png

In case you have not installed python in your BaseOS (RedHat). You can install the python and libraries like this:

yum install python3

pip3 install joblib

pip3 install scikit-learn

pip3 install pandas

I prefer jupyter notebook to run my code for this, you can install jupyter notebook by simple command pip3 install jupyter. And can start jupyter notebook. 20 Jupyter Run.png

This will open a browser for you where you can manage your files. Create a new python3 file and it will redirect you to new Tab. Enter the code below and execute the code line by line by using the shortcut: "Shift+Tab".

22 Run Code.png

How to Load Model on Container?

After using the dump function of joblib library, we will observe a new file being created in the same directory where we started the jupyter library. This file is our trained model. We will use this file and load it in our Docker container. To load the file on Docker container we run:

docker cp salary_model MLos:/salary_model

23 Load Model to container.png Hence we were successful to Load our own machine learning model on top of Container.

How to Test our Model on Container?

Now we go in our container and check whether our model is loaded or not by ls command. After that we will use our model to predict salary corresponding to years of experience.

24 tEST mODEL.png

We have successfully tested our model. Hence our task of Docker Container Integration with Machine Learning Model in Python is complete.