Machine learning model of simple linear regression on Docker

Shreya Mehta
5 min readMay 30, 2021

In this article we are going to make a machine learning model in python language using a salary dataset and deploy that into docker.

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 docker container
  • - In Container you need to copy/create machine learning model which you have created in jupyter notebook

Step1: Enable docker in your system

After installing docker in your system Rhel8 you need to make sure it is activated and if not enable it. By using the following commands-

systemctl is- active docker

This command is used to check if docker is activated in the system or not.

systemctl enable docker – now

This command is used to enable docker into the system.

system status docker

This command is used to show a detailed status of docker into the system.

Step2: Pull docker image in the system

We need to pull image of a container into a system inorder to work in it.

docker images

We use this command to check and see if any images are present in it currently.

docker pull centos:8

This command is used to pull image of container into the system. Here we are using the latest image available of centos:8 from public repository dockerhub.com which contains all the images for the docker.

docker ps

This command is used to check which container is in the running state

Step3: Creating a container

Now we need to create a container inorder to start working in it.

docker create -it – name MLOPS centos:8 /bin/bash

This command creates a container named as MLOPS inside docker image that we pulled.

docker ps -a

We use this command to see which containers have yet started and stopped

docker start MLOPS

With this command, our container has started functioning so next we will attach it to the system inorder to work inside it.

docker attach MLOPS

With this command now we’re inside the container and can start working in it.

Step4: Installing python and git

Since we are making a machine learning model and our code will be in python we need to install python inside this container.

The code and dataset used to make machine learning linear regression model Ive stored in the git repository so we need to install git as well inside this container.

yum install python38 git

This command installs python and all the latest git softwares.

Step5: Clone git repo

We need to clone the git repo inside this container inorder to have access at it and work on it

git clone <github repository url>

This command will clone all the repo files into this container inside docker.

ls

We use this command to check what is inside the container and this way we can check even if it has been successfully cloned since we’ll see all the git file names now.

cd SalaryPredictionML/

When we cloned the git repo it gets clone into a file name so we enter that file with this command cd <Filename> ie we’re changing directory inside container.

ls -a

We can view all files in this directory now.

This is my github repo which contains the dataset and all other code files.

https://github.com/shreyamehta83/SalaryPredictionML

Step6: Executing the model

Since we have successfully cloned the repo inside the container now we can execute our machine learning model now.

For that we have to install few libraries before which we have used inside our python code to work.

I have written the libraries needed inside a txt file named requirements and I will send a command to install it.

pip3 install -U -r requirements.txt

This command will install all the libraries needed that we named in the file before.

Now that all the libraries are installed we can successfully execute our program.

python3 Model.py

This executes our main machine learning model code

ls- la

This command shows all the new files that have been made when we run our model inside the SalaryPredictionMl directory because when we execute our main model Model.py in it, we made a new file as SalaryPrediction.pkl as a pickle file of that model.

python3 salary_predict.py

We’re executing another .py file in which we make use of pkl file.

Step7: Stopping the container

Since we have successfully executed the machine learning model inside docker we can now stop the container by using the command

docker stop MLOPS

inside our root terminal to stop the container.

docker ps -a

To check if the container has successfully stopped and see all the container id and its details.

docker rm<container_id_MLOPS>

This command is used if we want to delete the container.

I would like to thanks Vimal Daga Sir for giving an important topic to research and do the practical and figure out the importance of this architecture.

--

--