Run a Machine Learning Model in Docker Container

Nithish Kumar
3 min readJul 16, 2023

--

Running machine learning models in Docker containers offers numerous advantages, making it a popular choice in the industry. Docker provides a lightweight, portable, and isolated environment for deploying machine learning models, enabling seamless integration with various data pipelines and real-time applications. Here I’m building every thing from scratch.

First we need to install Docker in Base OS (In my case, it is RHEL 9). For Installing Docker, we have to add repo of Docker. To add repo, install yum-utils.

sudo yum install yum-utils
# Adding repo of Docker.

sudo yum-config-manager — add-repo https://download.docker.com/linux/centos/docke
r-ce.repo

Now Install docker using below cmd:

yum install docker -y

Start the services using systemctl command

systemctl start docker

# check it is working or not
systemctl status docker

Run a container so that we will test our salary model (I’ve downloaded it from internet). Here I’m using centos 7 version.

docker run -it — name salary_model centos:7

Install python and required python libraries for machine learning model.

yum install python3

pip3 install joblib

pip3 install sklearn

Now I’m copying the downloaded model to docker container using docker cp command.

To predict the salary model, I’ve written some python code which will take input as experience and give output as expected salary based on experience.

# py-salary.py

import os
import joblib
mind=joblib.load(‘salary.pk1’)
os.system(“clear”)
print(“Welcome”)
mind=joblib.load(‘salary.pk1’)
i=float(input(“Enter Years of Experience: “))
print(“Predicted salary of the employee is “,mind.predict([[i]]))

Now execute the python file

python3 py-salary.py

Note: both python file and salary machine learning model should be in one folder.

…Signing Off…

--

--