ML Model Deployed Inside Docker Container

Rohan Parab
4 min readMay 27, 2021

This article shows the example of deploying a normal linear regression model inside a Docker Container.

Here, I am using a Red Hat Linux 8 as the Base OS and on top of that making Docker host and making a Centos Docker image containing all the required files for the ml model using a Docker File.

Dockerfile is used to create a required docker image using a script.

what is Linear Regression?

Linear Regression is the method to predict a value of an independent variable using dependent variables, weights and bias. The module that provides LR is scikit-learn.

What is Docker?

It enables developers to package applications (in our case, a Machine Learning Predictor Script )into containers — standardized executable components that combine source code with all the operating system (OS) libraries and dependencies required to run the code in any environment. In simple terms, we would be able to run a Machine Learning Code in a separate OS with only the requirements needed for our script to execute.

Let’s Begin

> Create a separate directory for the project

# mkdir task1/

> python script

I had prepared a python script that will make prediction for the profit made by the startups considering the parameters ‘R&D spend’, ‘Administration’, ‘State’ using linear regression.

# cat profit_predict/profit_predict.py#!/usr/bin/python3import joblib
import pandas as pd
model = joblib.load('profit.pkl')
ohe = joblib.load('state_ohe.pkl')
class Profit:

def __init__(self,data):
self.data = data

def predict(self):
d_data = ohe.transform(data)
predict = model.predict(d_data)[0]
print(f"This approximate profit made by the startup is: ${predict}\n")

if __name__ == "__main__":
print("************************")
print("Startup Profit Calculator")
print("************************\n\n")
rnd = float(input('Enter R&D Cost: '))
admin = float(input('Enter Administration Cost: '))
market = float(input('Enter Marketing Spend: '))
city = input('Enter State [New York,California,Florida]: ')
data = pd.DataFrame([rnd,admin,market,city],index=['R&D Spend', 'Administration', 'Marketing Spend', 'State']).T
obj = Profit(data)
obj.predict()

I had copy this file along with all the requirement modules and dependencies in the container.

requirements.txt is created by

# pip3 freeze > ./requirements.txt

so finally directory looks as follow:

Check Docker status:

Pull the docker image from the docker hub

docker pull centos:latest

Create a Dockerfile

Let’s create a Dockerfile to perform various operations inside the container w/o going inside the docker container.

note: create the Dockerfile outside the project directory “profit_predictor”

# cat Dockerfile FROM centos:latest
COPY /profit_predictor .
RUN yum install python3 -y && pip3 install -r requirements.txt
ENTRYPOINT ["./profit_predictor.py"]

The code states that the while creating the container the Dockerfile will use the pulled centos image. Then copy all the files from /profit_predictor in current folder. Then install python3 in container and the requirement.txt file. And at the end location of the executable script as the entry point of the container.

Now build the docker image using Dockerfile

# docker build -t task1predictor:v0.01

The image has been created successfully as you can see

Run the Container

docker run -it -- name test1 task1predictors:v0.01

Thus, I had successfully run the ML model inside the docker container..

You can get the code at github:

Meet me at :

--

--