Training ML model inside Docker Container

What is Docker ?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.


Containers are an abstraction at the app layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in user space. Containers take up less space than VMs (container images are typically tens of MBs in size), can handle more applications and require fewer VMs and Operating systems. one of the eg. is Docker Container Engine.

What is Docker Container ?

Docker is a tool by which we manage the container , container is like a operating system you can launch these container with in a second ,docker work on the top of these container’s.

How does Docker work?

The Docker technology uses the Linux kernel and features of the kernel, like C groups and namespaces, to segregate processes so they can run independently. This independence is the intention of containers — the ability to run multiple processes and apps separately from one another to make better use of your infrastructure while retaining the security you would have with separate systems.

Container tools, including Docker, provide an image-based deployment model. This makes it easy to share an application, or set of services, with all of their dependencies across multiple environments. Docker also automates deploying the application (or combined sets of processes that make up an app) inside this container environment.

These tools built on top of Linux containers — what makes Docker user-friendly and unique — gives users unprecedented access to apps, the ability to rapidly deploy, and control over versions and version distribution.

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

Steps to do the Task :

  • Pulling the CentOS image from docker hub.
  • Creating new Container using CentOS image.
  • Installing python software on the top of new container
  • Importing libraries required for training ML model.
  • Copying the data file to the container.
  • Train and load the model in one file to predict the values.

lets , Start

  • Start the docker service
$ systemctl start docker
  • Pulling the CentOS image form docker hub
$ docker pull centos:latest 
  • Creating new Container using CentOS image with name ML-model
$ docker run -it --name ML-model centos:latest

using the above command we can directly interact with the container as we used -it option in the command

  • Installing python software on the top of new container

As it is new container we have to check as there python software present or not if not the use below command to install python software.

# yum install python
  • Importing libraries required for training ML model.

Now we have to import some libraries required to train our model. for this we using pip3 command to install those libraries.

# pip3 install numpy
# pip3 install pandas
# pip3 install sklearn
  • Copying the data file to the container.

we have to copy the data that is present in our base OS i.e RedhatOS to the centos container to do this we run below command in the base OS .

note: In this case you can copy your trained model to run in container or we can copy dataset to train that dataset and to create model inside the container In this case I copy dataset and creating model inside the docker.

docker cp <src> <container_name>:<dest>

$ docker cp Salaray.csv ML-model:/task1/
  • Create the file for Training model and save the model.
# vim
  • Now, run and put the values for which we want to predict. In this case I trained model in which we have to give the no. of experience years so , it will predict the expected salary of that no. of years.
# python3

Here our model is created and predicted properly on the top of container.

so ,

!!! Task Completed Successfully !!😃😃😉

!!! Keep Sharing !!! keep Learning !!!

!! Thank You for Reading the Article !!

Arth Learner