A Machine Learning Model inside the Container


In this article, I am going to show you how to create a machine learning model inside the docker.


  1. pull the centOS image from DockerHub
  2. Install the Python on the container
  3. Create the ML model inside Docker

Firstly, we have to check that the docker is installed or not so we will use this command.

docker --version

To use docker we have to start docker services and I have used this command

systemctl start docker

Now, we have to pull the centos image from DockerHub by using this command.

docker pull centos

To check if our os is download or not we use “docker images” command and to run the docker container we have used this command

docker run -it --name centos

Now we are inside our container and we have to install some software for running the ml model.

first, install the python by this command

yum install python3

Now we have to install the NumPy library

pip3 install numpy

For loading the dataset we need pandas library so using pip3 we have to install pandas library

pip3 install pandas

We have to install scikit-learn library to use linear regression .

pip3 install scikit-learn

We have installed all the library which is important to run our program

  • I have used WinSCP to transfer my data from window to RedHat

Now my dataset “Salary_Data.csv” is there in RedHat.And we can check by ls command

Now we have to send this file for our base os to docker by using this command. This command we have to write in our base os terminal.

docker cp Salary_Data.csv mlmod:/root/project

Now Salary_data.csv is copied in the container and now we have to write our Machine learning code and also we have to train our model.

Now let's create a test.py

“vi test.py”

import pandas as pd 
import numpy as np
dataframe= pd.read_csv('Salary_Data.csv')
x = dataframe['YearsExperience'].values.reshape(30,1)
y = dataframe['Salary']
from sklearn.linear_model import LinearRegression
model = LinearRegression()
import joblib

python3 test.py command will train the model

vi ml_model.py

import joblib
num=float(input("years of experience:"))

The code can be found on GITHUB.