Akanksha Singh

May 31, 2021

6 min read

Training and Testing Machine Learning Model inside Docker Container

docker containers provide quick environment setup to deploy any product hence we are using this to train our model and then predict by providing some dummy inputs…

Machine Learning:

Machine Learning models does data analysis and create a pattern known as Formula for either Linear or Classified data. So there are mainly two types of ML models:
1. Regression Model : ML models for regression problems predict a numeric value. For training regression models, ML uses standard learning algorithm known as linear regression.

Docker :

Docker is a Containerization tool that helps user to launch an Operating System from a pre-booted image and then use the image for testing or Quality Assurance of the product. Also for Automation and Orchestration we use these Containers. Docker tool itself has many capabilities that ensure the volume attachments, networking by using Porting and also copying, pushing and pulling image from docker Repository (hub.docker.com) and many more.

Fig 1. dataset.csv File

ML-Salary-Estimator-Python Code:

Following is the Python Code that Estimates the Salary of the Employer having some year of Experience. It Estimates salary according to previous pattern it analyze from the historic dataset that we have given in the form of dataset.csv file.

Fig 2. ML-CODE File
  • Loading of Data
  • Fetching relevant independent features for finding prediction pattern\formula
  • Loading Linear Regression function as we have linear data in hand (dataset.csv)
  • Fitting the value of predictor (x) and predict (y)
  • This whole training we are doing here can be just done once and after that we can save the model output weight and bais to load and use. (Suggestion)
  • At the end it would ask user about entering the product or exit with ‘Y’ or ’N’ keys
  • Then Prediction function would work as user input the ‘Year of Experience’ of new joining employ to estimate salary
  • We have options to continue working or ‘exit’ when work is done.

Pre-requisite :

◾ Linux Machine installed in Base OS or VM.
◾ Docker installed over that OS

Let’s Discuss the Problem Statement:

  • We have to pull Docker Container image of Centos from hub.docker.com and further create a container from that image.
  • Inside that Launched Container
    1. Install Python3 software
    2. Install python modules scikit-learn, numpy and pandas using “pip3”
  • Copy the ML code that we have created for Salary Prediction inside the container and then deploy it. We can do this in 2 ways:
    1. by “docker cp” command to copy code directly from base OS.
    2. by “git clone” as we pushed the code and data to GitHub repository for further use.
  1. You can See I have my docker.service running in Rhel 8 Linux Machine
Fig 3. docker daemon active
# docker pull centos:7
Fig 4. Centos Images present in my System
Fig 5. Lunched esti-con container
# yum install python3 -y
Fig 5. Installed python3 software
# pip3 install scikit-learn numpy pandas
Fig 6. installed all the required library
# docker cp <source_path> esti-con:<destination_path>
Fig 7. We have copied both dataset and code to the docker container
Fig 8. Execution of our ML Program
(: — )

Thanks for reading. Hope this blog have given you some valuable inputs!!