Running ML Code Inside Docker Container

Suyash garg
3 min readMay 27, 2021

Introduction

Hello everyone in this article i am going you to show how to run ml code inside docker container

In this article I am going to:-

  • Pulling Centos image
  • Running container with centos image
  • Installing pip
  • Installing required libraries
  • Copying code and dataset in the running docker container
  • Running code
  • Saving container as the new image

So lets get started

Demo

Pulling Centos image

To pull centos image use

docker pull centos

Docker pull is the command used to pull images from the repository docker is already configured to use hub.docker.com so we don’t have to worry about how to configure image repository

Running container

Use following command to run the docker container

docker run -it --name myos centos

docker run command is used to create the container

-it:- stands for interactive terminal means as soon as the container launch give the terminal of that container

name:- option is give so that we can give name some name to the container its good practice to give the name to the container its helps us to manage container

centos is the name of image

Installing pip

To install pip use following command

yum install python3-pip -y

enter y we yum ask yes or no

Installing required libraries

use following command to required libraries

pip3 install scikit-learn pandas

Copying code and dataset

We can also create the code inside the container bur for this demo i am going to copy the code inside the container for this open new terminal and write the following commands

docker cp /root/Documents/ml.py myos:/
docker cp /root/Documents/SalaryData.csv myos:/

docker cp command is used to copy data from one base os(docker host) to container

Running code and saving model

Go to previous terminal and to run the code use the following command

python3 ml.py

Saving container as the new image

Now use exit command to comes out the container

now use following command to create the new image from the container

docker commit myos mlimage

now run docker images command to confirm weather image is created or

as you can see i my case the image is created

Closing Note

Thank for everyone to read my article till end if you have any doubt please comment if you have any suggestion please mail all comment both positive and negative is more than welcomed.

Download all the code and data set from here:- https://github.com/suyash222/mlopstask1

Contact Detail

LinkeDin [https://www.linkedin.com/in/suyash-garg-50245b1b7]

Additional Tags

#machinelearining #summerintership #righteducation #vimaldaga #worldrecordholder #docker #mlops

--

--