How to Run Machine Learning code in Docker Container ?🤔

Avijit das
3 min readMay 27, 2021

✏️ What is Machine Learning ?

Machine learning 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.

✏️ What is Docker ?

Docker is a set of platform as a service 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.

📍 Let’s see how can we use Machine Learning code inside Docker Containers :

In this blog I am going to use Linux ( RHEL 8 ) as a operating system with root user.

💎 1. First we are gonna launch a docker container with “ docker run -it --name ML centos:latest ” command. We are using latest version of centos as image for container. To download centos image use “ docker pull centos ” command.

docker run -it — name ML centos:latest

💎 2. Now install python3 with “ yum install python3 -y ” command.

yum install python3 -y
yum install python3 -y
yum install python3 -y

💎 3. Now install required libraries for Machine Learning with pip3 command. Libraries like : pandas , scikit-learn. To install pandas use “ pip3 install pandas”.

✒️ We can download any python library required and use it. For writing the code we can use vi editor or vim editor. If vim editor is not installed we can download it with “ yum install vim ” command.

📌 Now I will write a machine learning program to run inside the docker container.

💡 1. I will create a directory named “ ML ” for writing our program and we will be using Salary_Data.csv file as a dataset. To download the same dataset :- https://github.com/AvijitD22/Summer-2021-Task-1

💡 2. We will use vi editor.

vi ML.py

💡 3. Now we will write our machine learning code. You have a look at the code below. Below code :- https://github.com/AvijitD22/Summer-2021-Task-1

💡 4. Let’s test the code. To run the code use “ python3 ML.py”

python3 ML.py

GitHub URL of ML code and Dataset : https://github.com/AvijitD22/Summer-2021-Task-1

Thanks For Reading🙏.

--

--