Machine Learning Model on Docker

This article will give you the general idea about the deployment of ML model inside a Docker Container, but you can use this method to deploy any ML model inside a Docker Container. This will be easy and fun to integrate two technologies together. NOW LET’S GET INTO IT !!

We will be using Red Hat Linux 8 as the Docker host and Centos Docker image which will contain all the file for the ML model. Here the Machine Learning model which we are going to use is Linear Regression model

Now, Let us first understand, what is LINEAR REGRESSION and DOCKER ?

What is Linear Regression(LR) ?

Linear Regression is used for finding linear relationship between two continuous variable. One is target or dependent variable and other is one or more predictor or independent variable. Relationship between two variable is said to be deterministic relationship if one variable can be accurately expressed by the other. The module that provides LR is scikit-learn.

What is Docker ?

Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application. Here we will be able to run a Machine Learning Code in a separate OS with only the requirements needed for our script to execute.


  1. We need a dataset and the dataset that we are going to use is “Salary_Data.csv”. Using this dataset we are going to train our Linear regression model to predict the Salary based on the Experience given by the user.
  2. We need libraries like pandas to import the dataset , scikit-learn to import the Linear regression model, joblib to save the model.

Using these two steps we going to create a train LR model.


  1. First we need to install Docker but here i have already installed the Docker and then launched one container named ML_model as using the image of CentOS: latest.
Run the Docker Container

“systemctl status docker” command will tell you if docker is running or not.

Docker status
centos:latest image is used to launch the docker container

2. Dataset csv file, saved file of the trained model, and the code of training all these three files have been transferred here from windows to RedHat using WinSCP. WinSCP needs the IP address of your RedHat only then you can transfer data.

Transferring the files from windows to Redhat OS

3. Using joblib the model the that I have saved inside the file called “Salary.pkl” which save the data in binary and the “” which contain the user interaction code and lastly the “requirement.txt” a text file that we create to install all the required libraries inside the container all them are going to be copied inside the container are shown below. Using this command

$ docker cp file_name docker_container_name:/
Copying inside the container
Before copying
After copying

The image shows the contain inside the “”

3. Now we are going to install the python3 and for installing all the required libraries we going to use “requirement.text”

install the python3
installing all the libraries

6. After all the installation we are going to run our “” and then I’m going to give a input of 15 years of experience just to see how this prediction will go.

As we can see the result we get is 166468.72.

I hope that you had enjoyed the article and learned a new concept today :D

The code for this practical could be found out at: