Training and Deploying Machine Learning Model in Docker Container

Nikita Sahoo
3 min readMay 27, 2021

What is Docker?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

What is Machine Learning?

Machine learning is a method of data analysis that automates analytical model building. It is a branch of artificial intelligence based on the idea that systems can learn from data, identify patterns and make decisions with minimal human intervention.

In this article, we are going to run machine learning code on top of the docker container.

I am using an AWS EC2 instance here.

First, let’s install python

yum install python3 -y

Configure the repository for docker:

Create a file “/etc/yum.repos.d/docker.repo”

[dockerrepo]
name=docker repo baseurl=https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck=0

Installing Docker :

yum install docker-ce --nobest -y

Starting the docker service:

systemctl start docker

Checking the docker service status:

systemctl status docker

The Docker service is running all fine now we are ready to launch the centos container

We are going to use Centos image and deploy a container.

docker pull centos:latest

It will pull the centos image and also launch the container.

Let’s Install python in the centos container

yum install python3 

We need to install some python libraries which we are going to use in the Machine learning code.

  • sklearn
  • pandas
  • numpy

Now we need to copy the “SalaryData.csv” file to the centos container in the root directory

docker cp SalaryData.csv os1:/

The SalaryData.csv is successfully copied.

Below is the salary prediction code:

Now let’s Learn this code

Above 74042.011800594 is the predicted value by our model.

--

--