Basic Machine Learning on Docker

Krithika Sharma
4 min readMay 28, 2021

--

Content

In this article, we will see
1. What is Docker
2. What is Machine Learning
3. Linear Regression model
4. Configuring yum in rhel8 for basic softwares
5. Configuring yum in rhel8 for docker
6. Installing docker community edition
7. Pull the Docker container image of CentOS image from DockerHub and create a new container
8. Install the Python software on the top of the docker container
9. Copy the dataset from rhel8 to docker container using docker cmd
10. Create the model inside the container
11. Save the model
12. And lastly, using the model for further predictions

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 branch of artificial intelligence (AI) and computer science which focuses on the use of data and algorithms to imitate the way that humans learn, gradually improving its accuracy.

Linear Regression:
Linear regression is a linear approach to modeling the relationship between a dependent variable(y) and one or more independent variables(x).

Let’s start. For this we are going to use rhel8 VM from the virtual box(this link is for windows) as the base OS for Docker container. Launching the redhat8 VM and attaching the rhel8 iso file(you can download from here). Mount the attached iso(/dev/cdrom) to any directory let’s say ‘dvd’ using the following cmds

#mkdir dvd
#mount /dev/cdrom dvd/
Confirm by
#df -h
to avoid mounting this iso everytime we can mention the mount cmd in rc.local file so that we don't have to mount it again n again
# vim /etc/rc.d/rc.loacl
in line 14(not complusory we can mention anywhere)
mount /dev/cdrom /dvd
Save this file using ':wq' in escape mode
change the mode of rc.local file to make it executable
#chmod +x /etc/rc.d/rc.local

Configure yum by creating any file with repo extension( let’s say MyRepo.repo) and with this content

MyRepo.repo file

This Repository is for normal software installations. To install docker we need to make another repository. However, it is not necessary to make the new file for this, we can even mention this in MyRepo.repo file too.

MyDocker.repo

Then install docker using

#yum install docker-ce --nobest
this if it throws error use --allowerasing option

check the docker info for details

We neither have any running containers(docker ps) nor any stopped containers(-a option shows all the container that are running and stopped)

Now to download the centos image from DockerHub use #docker pull <imgname>:<version>

If the version is not specified it uses latest by default

#docker pull centos
Check whether the image is downloaded or not using
#docker images

Launch the container using

#docker run -it --name python_ML_os:latest

Now, inside the container install python36, vim(yum will be already configured)

#yum insatll python36 vim

Install all the required python libraries using pip

#pip3 install pandas joblib sklearn

Copy the dataset from Rhel8(host) to the docker container. Before copying create a dir with any name let’s say task1(#mkdir task1)to manage the code n for easy maintenance purposes.

root@localhost#docker cp SalaryData.csv python_ML_os:task1
dataset

Now, lets use LinearRegression to train the model

salary.py(part1)
salary.py(part2)

Run the above python file

#python3 salary.py

Loading the above saved model in predictSalary.py file

predictSalary.py

Now, giving the experience and checking what is the salary for that experience by running the above code

#python3 predictSalary.py

Task Done!!!

Thank you! keep learning! keep growing! keep sharing!

Krithika Sharma
If you enjoyed this, follow me on Medium for more
Let’s connect on LinkedIn

--

--