IMPLEMENTING MACHINE LEARNING ON DOCKER CONTAINER USING PYTHON

Jayesh Jain
5 min readMay 27, 2021

🎆Task-1:

Task Description 📄

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

🍁 Install the Python software on the top of the docker container

🍁 In Container copy the python file from the host to the docker container.

🍁 Install required libraries required to load ML model

🍁 Run the python code for prediction.

The World of Machine Learning🤖

Machine learning is the science of getting computers to act without being explicitly programmed. In the past decade, machine learning has given us self-driving cars, practical speech recognition, effective web search, and a vastly improved understanding of the human genome. Machine learning is so pervasive today that you probably use it dozens of times a day without knowing it. Many researchers also think it is the best way to make progress towards human-level AI.

Docker🐋

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that the machine might have that could differ from the machine used for writing and testing the code.

So now that we understand a little about the technologies, let’s start the task in order to grasp it better.

🌿Step 1:

Configuring YUM and installing Docker-ce using the command: “yum install docker-ce”

🌿Step-2:

Starting the docker services. We can do so with the help of the command: “systemctl enable docker”

After starting the docker, we move onto the next step.

🌿Step-3:

Checking whether the services have been started or not. If some error occurs, we can disable the firewall in the OS and check the services again.

We check the situation of the Docker container using the command: “systemctl status docker”

Once the services are started, we shall proceed to the next step of the task.

🌿Step-4:

An Image for the container.

A Docker image contains application code, libraries, tools, dependencies and other files needed to make an application run. When a user runs an image, it can become one or many instances of a container.

We will use the command: “docker pull centos:latest”. This command will help us in getting the latest CentOS from the internet.

🌿Step-5:

Running the image which we pulled from the internet.

The image we need will be run with the help of the command: “docker run -it — name CONTAINER NAME centos:latest”

When the command is successfully implemented, we will enter an image on top of the docker container.

🌿Step-6:

Installing Python3

Python is an interpreted high-level general-purpose programming language. Python’s design philosophy emphasizes code readability with its notable use of significant indentation.

We install python3 with the help of yum using: “yum install python3”

🌿Step-7:

Installing the libraries in the container in order to implement Machine Learning

A Python library is a reusable chunk of code that you may want to include in your programs/ projects. Compared to languages like C++ or C, Python libraries do not pertain to any specific context in Python. Here, a ‘library’ loosely describes a collection of core modules. Some of these libraries which help in Machine learning are NumPy, SciPy, TensorFlow, Pandas, etc.

In this task, we will only need three such libraries, namely, NumPy, Pandas, and Sklearn.

Here are the individual commands for the libraries alongside their picture for reference:

🏹For NumPy: “pip3 install numpy”

🏹For Pandas: “pip3 install pandas”

🏹For Sklearn: “pip3 install sklearn”

🌿Step-8:

After installing the libraries inside the image, we will come out of the container and transfer our data for the Machine Learning model.

We do so with the command: “docker cp FileName.csv ImageName:FileName”

NOTE: The CSV file or comma-separated values file is one of the most widely used flat files to store and share data across platforms. The columns are separated by a comma and there is an optional header row also which will indicate the name of each column. Python can read the CSV files using many modules.

🌿Step-9:

Inside the container, we will create a python file and write the code in that file to create our model to work on our data and predict the required result. The command for this is: “vi filename.py”

The code in this file is given below:

NOTE: We use .py format so that Python understand the format of the file created.

🌿Step-10:

Finally, here we are at the last step of our task! We will run our code inside the container using the command: “python3 filename.py”

I would like to thank the reader of this article to set aside a sliver of your valuable time and gaining some knowledge about the topics discussed here.

Feel free to ask any queries. Don’t forget to share this article with your fellow acquaintances. Connect with me on LinkedIn using the link provided below:

https://www.linkedin.com/in/jayesh-jain-0522061aa

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response