Deploying a ML model inside Docker

Deploying a ML model inside Docker

In this blog, we'll be talking about deploying a trained Machine Learning Model inside a Docker Container.

So, for this, we first need a dataset, and here I took a simple dataset for marks. It is simple because we don't need to do much of data preprocessing, data engineering and all. To make deploying simple and again this is about deploying a model into docker and not about machine learning, so I took a simple dataset. The dataset looks as below:

No alt text provided for this image

The training of this model is also very simple, as it does not contain any strings, labels, null values, so we can directly proceed into making the ML model. The code for training is available on GitHub, not a big deal. But here's the screenshots of code for the development of the ML model

No alt text provided for this image
No alt text provided for this image

The model has been created as of now, After this I'm just dumping the model into a package using joblib. So that we don't need to train the model again and again, we can just use this model 'marks_predictor.pk1' .

No alt text provided for this image

Now that the model has been created and dumped into a file named 'marks_predictor.pk1', we can just use it. Now the next step is to setup docker.

For setting up docker, I have used an AWS EC2 Instance, to make things simple, no need to setup yum and all, so yeah let's get into it. I used Amazon Linux 2 ami.

After you log into your ec2 instance, install docker and setup it with the following commands:

sudo yum install docker -y
sudo systemctl start docker
sudo systemctl enable docker

Now the docker container engine has been setup, we need an os for the container, here I am opting for CentOS:7. We can now collect the image and launch a container using the following commands:

docker pull centos:7
docker run -it --name <cont-name> centos:7

We will then land directly into our

Now we can install python3 and required python packages/modules in our container. The commands are:

sudo yum install python3 -y
pip3 install scikit-learn

NOTE: joblib is already included in scikit-learn, if note you can install it as well using "pip3 install joblib"

Now that all the dependencies have been installed, we can use our model, but before that we need to copy our model 'marks_predictor.pk1' into our docker container. We can do that using any method, i simply used github to upload and then git to download it into docker (for that i needed to install git as well in the container, using yum).

Now finally use your model by the following lines of code. I used live interpreter of python, you can write inside a file and then execute using "python3 <filename> "

No alt text provided for this image

That's it for this blog, you have successfully learnt and understood how to deploy a ML model inside docker. We can also build our own docker image and deploy with just once click/command " docker build and run ". Well that's for another time.

Thank you for reading.

Vishnuvardhan Morishetti

SDE @Zscaler | 2*ICPC Regionalist 2021, 2022 | Guardian @LeetCode (Top 1%) | Codechef 4*

3 年

????

回复

要查看或添加评论,请登录

Charan V.的更多文章

社区洞察

其他会员也浏览了