Train and Run ML Model inside a Docker Container

Krishna Pal
4 min readMay 27, 2021

--

Steps to be followed to achieve this task👨‍💻

👉 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 which you have created in Jupyter notebook

Pre-requisite

✔Any OS having Docker Installed.

✔Having a dataset.

To check whether Docker is installed in your system or not, run the following to command to check it out.

Command-: docker info

Now to start docker service type systemctl start docker command. After typing this command check the status of running docker service using command systemctl status docker

Now docker service started. It’s time to pull CentOS image from DockerHub to our Red Hat Enterprise Linux 8 System.

Command to pull any image-: docker pull <image_name>:<version_name>

To see the images that has been downloaded type docker images command.

So, required image has been downloaded. Now we can create container using this image.

Command to create container-: docker run -it centos : latest

Now new container running with a image name centos latest version.

Now, we need to install python3 inside this container.

Command-: yum install python3-pip

Now after python3 installation, install python libraries so that we can work on a dataset.

Install pandas library so that we can read or load the dataset that is created in excel file having .csv extension.

Command-: pip3 install pandas

Along with it numpy library also installed with pandas library.

Now install scikit-learn library which provides many useful functions and algorithms for creating ML models

After installation had been done our base environment is ready to use. Now we want a dataset inside a container to work upon.

So firstly upload your dataset on Github and download it inside a container using git clone command.

Before running above command make sure that we have git installed in container or not.

To install git in container here is the command-: yum install git

Here is the github repo link to download the dataset-:

Now we have our dataset inside a container.

So now, we have to create a python script file named salaryml.py to train our model and run it on our workspace environment.

For creating file we use vi editor -: vi salaryml.py

Here is the python code of model-:

Now run this file to check whether it is working or not. To run this file using command python3 salaryml.py

salaryml.py

Bravo.. Here our code has been runned successfully. Now here we need to create another python script file named salarypredict.py to predict the salary for some years of experience.

For creating salarypredict.py file-: vi salarypredict.py

Here is the python code-:

salarypredict.py

Now, run this file. Firstly it will load the model and predict the salary based on the years of experience.

Hurray!… Code run successfully. 😊

Now , we have successfully created ML Model and done Prediction inside a Docker Container.

Thanks for Reading 😍

--

--

Krishna Pal
Krishna Pal

Written by Krishna Pal

Engineering Student || Aspiring DevOps Engineer || Tech Enthusiast

No responses yet