How to create a Machine Learning model inside Docker container?

Ansh Kumar Dev
3 min readMay 29, 2021

Hello guys in this blog I will be telling you how we can create and run a Machine Learning model inside a docker container.

First of all, we need to install Docker we can do this by the following command

yum install docker -ce — nobest -y

once the docker is installed completely we are ready to pull a container image. but before that we have to start the docker using the following command:

system start docker

to make the service permanent we use this command:

system enable docker

As the docker has now started, we are ready to pull an image.

pulling an image basically means installing an os inside docker.

In this tutorial, we are going to pull centos os using docker. For that we need to run this command:

docker pull centos

once the os is installed we need to start it and for this, we use the following command:

docker run -it — name yourOsname(anyname you want) centos

Now we are in a brand new os inside a docker container. Here we can perform all the tasks which we are able to perform in any regular Linux os.

As this is a brand new os so it does not have any software installed in it. So we have to install all the software here again according to our needs. But as this is a docker container it has some advantages like it already has yum installed and configured so there is no need to configure it again here and we can easily use yum commands to install various software.

Now, first of all, we need Python to run any machine learning model and for that, we have to install it first. So we can install python here by using this command:

yum install python3

In the above image, we can see that now python has been installed with all its dependencies. Now we are all set to write our Python code for Machine Learning Model. So we are going to create a file inside this os using vim and in that, we are going to write our code for the model and afterward, we will run that file using python3.

As of now, we have successfully created a new file with ML code inside it. You can see the code for the model in the above image. I have used a dataset for making my model learn and I have imported that dataset to this new container from rhel8 using this command:

docker cp <dataset_file_name><container>:<src-path> <local-dest-path>

now we are all set to run our las t command which is to run our pyhton file.

python3 <file_name>

--

--