How to run machine learning codes in docker containers

Deepanshu Yadav
3 min readJun 11, 2021

🍤I have written a machine learning code in python which predicts the salary of a person by taking input the no. of years of experience he has. Let me show you how it works.🍤

👲Below is the code i have written. As soon as i run it it asks for the experience. The dataset for this code is also in the same folder as you can see with the name SalaryData.csv.

This is the code👇

Now lets give some value to it

👉So, this is how the code works. Now lets see how we can run the same code in a docker container.

đź”°we will create a docker file and will put this code in the runtime so that as soon as we launch a container from the image, it will run the code and ask for the experience!đź”°

For this, create a file with the name Dockerfile and put the below code into it…👇

  1. In this code, i just picked the centos image and first downloaded the required libraries i.e. pandas abd scikit-learn.

2. Then i copied the code and the dataset into the image and in the end just used CMD to run this code as soon as we launch container from this image.

Now lets make an image from this. use the below command for making image👇

Now it will build the image and then you can check it by using the command docker images.

🎇Now we are done! Just launch a container and our model will start training. it would take time according to your ram and cpu you have and then will ask you for the experience. lets see.🎇

so, this is how you can run any ML/DL code inside containers!

DM me for any queries. Thanks for your time :)

--

--