Deploying machine learning model with Docker on AWS Cloud.

ello Connections,

This article will see how we can create a Machine learning model inside the docker container. Before we start first, let’s discuss the basic terminologies that will help us understand the article better.

What is Containerization?

Containerization is defined as a form of operating system virtualization, through which applications are run in isolated user spaces called containers, all using the same shared operating system (OS).

What is Docker?

Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.

Why Docker?

Using Docker lets you ship code faster, standardize application operations, seamlessly move code, and save money by improving resource utilization. With Docker, you get a single object that can reliably run anywhere. Docker’s simple and straightforward syntax gives you full control. Wide adoption means there’s a robust ecosystem of tools and off-the-shelf applications that are ready to use with Docker.

What is AWS cloud and Instance?

Amazon Web Services (AWS) is a secure cloud services platform, offering computing power, database storage, content delivery, and other functionality to help businesses scale and grow.

An instance is a virtual server in the AWS cloud. With Amazon EC2, you can set up and configure the operating system and applications that run on your instance

What is Machine Learning?

Machine learning is an application of artificial intelligence (AI) that provides systems the ability to learn and improve from experience without being explicitly programmed. Machine learning focuses on the development of computer programs that can access data and use it to learn for themselves.

ML behind the scene uses Mathematics to calculate the weight or coefficient based on historical data. A mathematical formula that the machine used to predict salary in this article is,

y = b + wxwhere,y = dependent variable (what we are trying to predict)
b = Constant
w = weight/coefficient
x = Feature/independent variable (what we are using to make predictions)

AWS Linux does not have a Docker engine as default, to use Docker in the AWS environment we have to install docker manually. In this project, I have used python as a programming language for Machine Learning. Python is hands down the best programming language for Machine Learning applications due to the various benefits. Machine Learning applications involve complex concepts like calculus and linear algebra which take a lot of effort and time to implement. Python helps in reducing this burden with quick implementation for the ML engineer to validate an idea.

To run our Machine Learning model we need an OS, so here we are using Amazon Linux instance(from AWS) to run docker and on top of docker we will run our ML model.

Create Amazon Linux:

To create Amazon Linux on AWS click here.

Connect to your Linux instance using SSH:

ssh -i /path/my-key-pair.pem my-instance-user-name@my-instance-public-dns-name

Install docker

sudo yum update -y 
sudo amazon-linux-extras install docker
sudo yum install docker

To verify installation use this command

docker info
or
docker --version

Add the ec2-user to the docker group, so you can execute Docker commands without using sudo.

sudo usermod -a -G docker ec2-user

configure docker and install python

Start the Docker service:

sudo systemctl start docker

Check the status of docker service:

systemctl status docker

Enable docker service:

systemctl enable docker

Pull the Docker container image of CentOS image from DockerHub and create a new container

centos from docker hub

Pull centos from docker hub:

sudo docker pull centos:latest

Deploy centos and give a container name ML_OS

sudo docker run -it --name ML_OS centos:latest

Install the Python software on the top of the docker container

yum install python3 -y

Copying CSV data file to AWS instance

scp -i /path/my-key-pair.pem <CSV_file> my-instance-user-name@my-instance-public-dns-name

Copying File form Host OS to Docker Container

docker cp <file> <container_ID/name>:/path

Install required packages for ML model

pip3 install pandas numpy joblib sklearn

Creating a ML model

Use vi or vim editor to create python files.

salary_model.py

vim salary_model.pyimport pandas as pd
import joblib
salary = pd.read_csv('Salary_Data.csv')
x = salary['YearsExperience'].values.reshape(-1,1)
y = salary['Salary']
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x,y)
joblib.dump(model, 'exp.pk1' )

Creating a simple program for this model:

salary_prog.py

vim salary_prog.pyimport joblibmodel = joblib.load('exp.pk1')
x = float(input("Enter Years of Experience: "))
result = int(model.predict([[x]]))
print(f"Salary will be : �~B�{result}")

Create and train the model:

python3 salary_model.py

Run trained model

python3 salary_prog.py

GitHub URL: https://github.com/yagyandatta/ML-model_on_docker/tree/master

Thank You :)

Devops || MlOps || Flutter || Web Development || PYTHON || Data Science || AWS cloud || GCP || Azure