Running Machine Learning Model on Docker Container
Published: 2021-05-29 15:46:07

**The summary of what we're going to build:**

->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

Note: I will be using Red Hat linux as my Base machine.

**Starting Docker** 
To start docker use this command: `systemctl start docker`
To check the status of Docker use command: `systemctl status docker`![Screenshot 2021-05-28 at 11.13.42 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9j4bsj2iuke2z07h34o4.png)
 




**Installing centos on Docker**
To use the latest version of centos use this command: `Docker pull centos:latest`
![Screenshot 2021-05-28 at 11.15.57 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/082i2ovdpj5nnnefxn6b.png)
 
**Checking whether OS has been installed or not**
Command to check: `docker ps -a`
![Screenshot 2021-05-28 at 11.16.48 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gceb7tmummyccezhtzxy.png)
 
**Launch Docker Image**
Use this command to launch the OS which you pulled from the Docker registry: `docker run -t -i <os-name> ` or `docker run -t -i --name=<container-name> <os-name>`
In our case, we have installed centos so replace `<os-name>` with `centos`. You can give your own `<container-name>`
For example: `docker run -t -i --name=ML1 centos`
![Screenshot 2021-05-28 at 11.06.56 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jm0khgobhfkmapec4yp.png)
 
 
Another way to start a Docker image is by running this command: `docker start <container-name>` and then `docker attach <container-name`

**Container**
Now that we're inside the container, this means that we're basically in a different operating system(OS).
To check the information about OS use: `cat /etc/os-release`
![Screenshot 2021-05-28 at 11.20.30 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrwv71sqynqmsm5lf019.png)
 
**Installing required packages to create ML model inside container**
Python language: `yum install python3`
Scikit library: `pip3 install scikit-learn`
Pandas library: `pip3 install pandas`
vim module to edit files: `yum install vim`

**Create the model**
First create a model in your Host OS and then copy file to Container using the following command: `docker cp <path-of-model> <container-name>:/<model-file-name>`
Ex: `docker cp model.py ml1:/model.py`

![Screenshot 2021-05-29 at 12.37.35 AM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1j70jl5q2xqypjrxzmn.png)

**Train the model**
First create a csv file named `Salary_Data.csv` using `vim Salary_Data.csv` and enter two columns namely `YearsExperience,Salary` and enter values as shown below,

![Screenshot 2021-05-29 at 12.43.27 AM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tp3e3wfwzxa1ymdax0p4.png)
 

Next we need to train the model using command: `python3 model.py`

![Screenshot 2021-05-29 at 12.36.29 AM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wh2hmcxy1gv42kk16aym.png)

Our model is to predict salary based on the person's experience(in yrs)

![Screenshot 2021-05-29 at 12.46.30 AM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dudazejvwnqxyaz5zh8j.png)