ML model over Docker Container

Samar Pratap Singh
4 min readMay 31, 2021

--

Today we are going to create & run a Machine Learning model over a Docker Container. As the industry is moving towards automation, it is the need of the hour to work on containers, as Container is of the most important automation & DevOps tools. So, let’s start…

Task Description:

  • 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 create a machine learning model which you have created in Jupyter notebook

Brief about Technologies & Keyword we will work with:

Pre-requisites:

  • A Linux base OS required, here I’m using RHEL8 by RedHat
  • Docker should be properly configured on the base OS

Let’s start:

  • Firstly start the Docker Services using the following command:
# systemctl start docker
  • And to check whether the Docker Services are up or not, then use this command:
# systemctl status docker
  • Now pull the Centos image from the Docker Hub:
# docker pull centos:latest
  • Then launch a Docker container using this Centos image. I’ve named the container “myos”, you can name anything. Options “i & t” will provide us a terminal to interact with the Docker OS launched.
# docker run -it --name myos centos
  • Now as we have successfully launched the Docker container & we have got the terminal of that OS too, so let’s move forward.
  • Now let’s download the required packages or software like: python3 & git. Then we will also require some python library for our ML code to run like Pandas, scikit-learn.
  • As I’m using RedHat OS, hence here I’ve to use “Yum” for downloading various packages.
  • Now download the required python libraries using pip3 command:
  • Now clone this ML code or whatever code you have in this Docker OS using the below command. Remember to install all the required libraries used in your python program otherwise, it won’t work.
# git clone https://github.com/Samarps/Salary_Predictor_Years_of_Exp.git
  • Various ML codes would be present in this repo along with the DataSet.
  • This code will basically predict that what should be the salary of an employee based on his/her years of experience.
  • So, firstly we need to train the model using the Dataset present. For this run “train_model.py” program.
# python train_model.py
  • Now the model is trained & saved. Let’s use our trained model to predict the salary by input of years of experience. For this run the “salary_predictor.py” program.
  • I’ve checked the model by inputting various values. If we will use better & bigger Dataset, it will provide better accuracy & results.

So, Finally, we have created an ML model using Python which runs over Docker Container. Docker is one of the tools used for Automation & DevOps used nowadays, so it is the need of the industry to shift everything over containers as they are very fast as compared to other alternatives like Baremetal, Virtualization, etc.

Hope you learn a lot by reading this article. Thank you for reading!

--

--