Execute Python Code On Docker Container

Niharika Dhanik
4 min readMay 30, 2021

Cheers To New Beginnings!

Welcome Back To A New Series Of Blogs Wherein I Will Be Posting Tasks From My New Internship + Training Called “MLOPS Online Summer Training Program 2021”

The coming 45 days are gonna be life changing for me. So taking one step at a time, I will be show-casing all that I will be learning throughout. Excited for all the new tasks, challenges and developing unique vision towards problem solving. So without any further due, let's hop on to the first ever task of this series.

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 docker container.
  • In Container you need to copy/create machine learning model.

Summary :-

Here, I have mentioned the list of resources we are going to work with along with their explanation.

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.

Python is an high level, interpreted, interactive, object-oriented programming language. It is a general-purpose coding language which means that, unlike HTML, CSS, and JavaScript, it can be used for other types of programming and software development besides web development. That includes back end development, software development, data science and writing system scripts among other things.

Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning.

Solution :-

🔲 Step 1 —

Setup yum and docker repository. And then install docker.

Commands used —

systemctl restart docker

systemctl status docker

🔲 Step 2 —

Pull centos image and start the newly created docker container

Commands used —

docker pull centos:latest

docker run -it centos:latest

🔲 Step 3 —

Install python and then using pip download all the modules/libraries that can be required for machine learning in python

Commands used —

yum install python3

pip3 install pandas / pip3 install scikit-learn

🔲 Step 4 —

Write a python file for extracting the information from the database(csv file) and creating a linear regression model. Dump the information from the model inside another (.pkl) file using joblib.

Note — the csv file has been copied from the host system into the container. The db can also be created manually but here I'm using pre-created CSV file.

🔲 Step 5 —

Make another python file and write the code to make predictions. Enter the value based on which the prediction analysis will bring result and run the file.

Finally, we have created a machine learning model on docker container using python and its various modules.

Thank You!

--

--