Deploy Machine Learning model on Docker Container

Aditya Raj
4 min readMay 31, 2021

Once the Machine Learning model is created then we need to deploy it somewhere so that our client can use it but how? So in this blog, I am going to explain to you how you can deploy your Machine Learning model on a Docker container, and later you can integrate it with Mobile Apps or Web Apps for your users to use it.

Docker:

Docker is an open-source tool designed to create, deploy, and run applications on containers. We can pack applications with all their libraries and dependencies and can deploy them as one package. Docker is like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications to be shipped with things not already running on the host computer.

For this practical, I have created one Linear Regression model to predict the salary based on the years of experience and then I have saved the weight and bias in a file. Now I will use the trained model to deploy on Docker.

Prerequisites

Linux Operating System: We need a Linux OS to install and run docker and for this practical, I am Amazon Linux AMI but you can use any Linux Operating System but you can use any operating system process is same.

Once the instance is up copy all the code and model to the instance with the help of WinSCP or SCP command.

Step 1: Install Docker software on Host Operating System.

First, we need to install docker software so that we use the docker service to launch the container, and for this, we can use the yum command.

yum install docker -y

Now start and enable the docker service so that we can use docker to launch the container.

systemctl start docker

systemctl enable docker

As the docker service is running now so we can use docker to create a container image and also launch the container with the image.

Step 2: Create a customized docker image with our application and saved model

In this step, I am going to create a container image with the help of Dockerfile in which I will copy the saved model and also the python code which will load the model.

Here is the code of Dockerfile to create a container image.

I have used centos:latest image as a base image and also I have made / as the working directory. Then I have installed the required packages and library for the python code to load the model to predict the salary. Also in CMD, I have written the command to run the python code to predict the salary. So whenever the container will be launched it will code to predict the salary.

Now let’s run the docker command to build the image.

docker build -t model:v1 .

Now as the image is created so we can launch the container to use the salary prediction application.

Step 3: Launch container to predict the salary

We can use the image which is created with Dockerfile to predict the salary of an employee based on the year of experience. Use the docker run command to launch the container.

docker run -it — name modelos model:v1

Here I have created one CLI Application that will ask for the experience in years and will predict the salary. But we can use a similar approach to use the model with the Flask app to create Rest API which can be used by Web Apps or Mobile Apps.

You can check my GitHub repository to get the complete code of the model, Salary prediction CLI Application, and Dokerfile.

GitHub Link: https://github.com/adyraj/ML-Docker.git

Thank You for reading!! 😇😇

--

--

Aditya Raj

I'm passionate learner diving into the concepts of computing 💻