Containerizing ML model — Task0 1

Mayank_Agarwal
3 min readSep 6, 2021

In this article, we’ll see how to containerize any Machine Learning model using a Docker container.

📄 Task Objectives

👉 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 jupyter notebook

Let’s get started…

STEP 1: INSTALL DOCKER

To install docker follow part 1 of the following blog,

Configuring HTTPD Server and running Python code on Docker Container…

What is Docker?

sangeethsahana.medium.com

STEP 2: START and check the SERVICE of Docker

systemctl start docker
systemctl status docker

STEP 3: PULL the Docker Image of centos

docker pull centos:latest

STEP 4: LAUNCH a docker container using the run command

docker run -it --name <name for your container> centos:latest

After launching the OS, you will be inside your container, install the dependencies for your ML model,

To Download:

→ Python

yum install python36

→ scikit-learn

pip3 install scikit-learn

→ git

yum install git -y

I’m downloading git, to download the dataset in the docker container.

→ pandas

pip3 install pandas

STEP 5: Download your dataset in the docker container

I’m putting my dataset in the docker container with the help of the git command. First, I’ll put the dataset and other code into my Github repository and then clone the repository in the Docker container using the git clone command.

git clone <your git repo url>

The file contents are as follows:

Dataset : salary.csv

result.py :

model.py :

STEP 6: RUN your python files

python3 model.py
python3 result.py

FINAL OUTPUT :

Finally, the model is trained and we got the Desired Result — Salary Prediction model !

OPTIONAL

You can even create an image of this(your) container and use it for future purposes

First, commit your image then tag and push into the docker hub

docker commit <container name> <image name>:versiondocker login
docker tag <image name>:version <docker repo name>
docker push <docker repo name>

Successfully pushed now you can pull your own custom image and use the ML model whenever needed.

--

--