Loading an ML Model on top of a Docker Container
What is ML?
Almost everyone, in the field of computer science at least, has come across the term Machine Learning or ML for short, most of them even have a brief idea about it, but what it means and how to implement is known to only a very few of the masses.
In simple words, Machine learning is a part of artificial intelligence that enables a system to learn from the data provided by it. The data provided to it is known as a dataset. Machine learning algorithms build a model based on sample data, known as “training data”, to make predictions or decisions without being explicitly programmed to do so.
What is Docker?
Docker is a set of the platform as service products that use OS-level virtualization to deliver software in packages called containers.
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. Available for both Linux and Windows-based applications, containerized software will always run the same, regardless of the infrastructure.
What is our TASK?
Our task is to create a machine learning model in python and then load the model on top of a docker container. Using a docker container may have a lot of reasons but the primary reason is using a docker container not only saves time but also saves memory.
For our current task at hand, we have a dataset containing the salary data of some employees based on their years of experience in the field. Our goal is to predict the salary of a new employee based on his/her years of experience in the field. We will use python3 for the programming and centos 8 as our docker container OS.
I am using RHEL8 (Redhat Enterprise Linux) as the base OS to install docker on top.
NOTE: If you are using the Linux OS on top of another OS such as Windows using Virtualization software such as Virtual Box,VM-Ware, etc. , you need to copy/transfer your dataset file to the Linux OS. One of the ways to do this is by creating a shared folder using the virtual box OS settings. (There are many different ways to transfer a file from the base OS to the VirtualBox OS if this method, for some reasons, does not work in your case then you can try for other methods too.)
Following is the step by step instruction to perform this task:
STEP 1: Install Docker
Step 1.1:
Go to the root account of the OS and then go to cd /etc/yum.repos.d/ and create a repo file for docker. Use the vim editor (any editor of your preference) to create a docker.repo file (you can give any name to the file just the extension should be .repo)
cd /etc/yum.repos.d/
vim docker.repo
Step 1.2:
In the vim editor, press i to enter in insert mode and then write exact lines and save the file by pressing esc key then :wq and then enter.
Step 1.3:
Now update the yum repository using the command yum repolist or dnf repolist (only in RHEL8) and see the docker repo is successfully made.
dnf repolist
Step1.4:
Now Download the docker-ce software that is the community version. Use command
yum install docker-ce –nobest -y
OR
dnf install docker-ce –nobest -y
Step 1.5:
Now we need to add firewall rules to activate masquerade, Port80, and 443 so that we won’t face any difficulty in Connectivity inside Docker Container.
Reload the firewall and check if all the ports and masquerade are added or not.
Step1.6:
Start and enable Docker services
systemctl restart docker
This restarts the docker services
systemctl enable docker
This enables the docker services on the system
systemctl status docker
This command is used to check the current status of the docker services
Step 2: Pull Docker Image
Pull centos docker image and launch a container named “myos1” using the centos image.
docker pull centos: latest
docker images
docker images command is used to list the current OS images in the docker
STEP 3: Copy the dataset onto the docker container
The docker cp command is used to copy the dataset from base OS (Redhat 8) to the docker container to a specified container and location.
docker cp SalaryData.csv myos1:/
The syntax for the docker cp command is
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|
OR
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
STEP 4: Create a new container
docker run -it — name myos1 centos: latest
docker run command is used to run a new instance of the particular OS. It’s syntax is
docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
NOTE: You can find the syntax of any command using the —help option after the command. Example docker run — help
STEP 5: Install Dependencies
Such as Python3/vim editor/etc. on docker container (CentOS:8)
Step 5.2:
Install all the required python3 libraries using the pip3 command
STEP 6: Creating an ML model in python
SalaryModel.py is the filename given to the python program for the ML model.
You can create this file by using the following command
vim SalaryModel.py
and write down the code
OR
you can import the python program onto the system like the dataset.
Note: The dataset used in this model is SalaryData.csv
STEP 7: Run the model
Use the following command to run the python program.
python3 <program_name>.py