💁🏻‍♀️ Training ML model inside Docker Container🐋

Vrukshali Torawane
4 min readMay 27, 2021

✨ What is Machine Learning ?

Machine learning (ML) is the study of computer algorithms that improve automatically through experience and by the use of data. It is seen as a part of artificial intelligence. Machine learning algorithms build a model based on sample data, known as “training data”, in order to make predictions or decisions without being explicitly programmed to do so. Machine learning algorithms are used in a wide variety of applications, such as in medicine, email filtering, and computer vision, where it is difficult or unfeasible to develop conventional algorithms to perform the needed tasks.

✨ What is Docker ?

Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels. Because all of the containers share the services of a single operating system kernel, they use fewer resources than virtual machines.

Task Description 📄

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

👉 Install the Python software on the top of docker container

👉 In Container you need to train machine learning model

Let’s Begin🤩

Step1 : Install docker

A ] Configure yum for Docker :

Create a file “/etc/yum.repos.d/docker.repo”[docker]name=docker repo baseurl=https://download.docker.com/linux/centos/7/x86_64/stable/

After this verify by “yum repolist” . You see some more software.

B] Install Docker and start service :

# Install docker
yum install docker-ce — nobest
# Start Docker service
systemctl start docker
# Enable Docker service
systemctl enable docker

Step2 : Pull centos image

docker pull centos:latest

Step3 : Launch a container

docker run -it --name <name_of_OS> centos:latest

Step3: Install required packages

# python
yum install python36
# installing required libraries for taskpip3 install pandas
pip3 install scikit-learn

Step4: Create a Docker image

docker commit <os_name> <image_name>:<version>

Step5 : Launch a container using that image

docker images

Step6: Copy the dataset

docker cp <location_in_localsystem>  <docker_os_name>:<location_in_docker_container>

In docker container :

Step6: Linear Regression Model

import pandas as pd
dataset = pd.read_csv('salarydata.csv')
X = dataset[['YearsExperience']]
y = dataset['Salary']
from sklearn.linear_model import LinearRegressionmodel = LinearRegression()model.fit(X,y)import joblib
joblib.dump(model, 'finalsalary2.pk1')

Step7: Model Created

Step8: Let’s Test !!

import joblib
model = joblib.load('finalsalary2.pk1')
yrsExp = float(input("Enter years of Experience : "))prediction = model.predict([[yrsExp]])print("Your Salary would be : ", prediction)

Thanks for Reading !! 🙌🏻😁📃

🔰 Keep Learning !! Keep Sharing !! 🔰