Running a Basic ML code on Docker Container

Rahul Bhardwaj
3 min readMay 27, 2021

Introduction

In this article we’ll be running a Basic ML code on top of Docker container, having CentOS image, on the top of Redhat Linux 8 virtual machine.

Setting up Container

Get the latest CentOS docker image from the online docker repository using

docker pull centos 

This automatically pulls the latest version of the image. After this start the container using

docker run -it --name myos centos:latest

Here we gave the name as myos and launched the container in an interactive terminal environment.

Check if the image has python or not using python or python3 command. Docker CentOS image usually doesn’t come with python3 installed so install is using

yum install python3 -y

After that you’ll need to download some libraries to run the ML program

pip3 install numpy pandas scikit-learn

Now you can open the interactive python3 prompt on your container and using python commands.

We’ll be using a simple dataset with just one feature for the demonstration. To copy the dataset from your local dataset to your container run the command

docker cp SalaryData.csv myos:/root/

In this myos is the container name and /root/ is the directory I want it to be copied to .

Copied successfully.

Starting with our Code

It is better to start with the interactive environment and then copy it into a file.

The overall code for our basic ML program is

import pandas dataset=pandas.read_csv('SalaryData.csv')
x=dataset['YearsExperience']
y=dataset['Salary']
x=x.values #converting into numpy format because we
x=x.reshape(30,1) #need a 2-D data
from sklearn.linear_model import LinearRegression
model=LinearRegression()
model.fit(x,y)
import joblib
joblib.dump(model,'mymodel.pk1')

After making your model you can store it using joblib.

The file will be stored in the file system of container. You can open your another python session and use the same file again.

Thanks for reading!

--

--