Train and Deploy Machine Learning Model inside Docker Container

Machine Learning

Machine Learning

Machine learning is an application of artificial intelligence (AI) that provides systems the ability to automatically learn and improve from experience without being explicitly programmed. Machine learning focuses on the development of computer programs that can access data and use it to learn for themselves.

Know more…



Docker is lightweight containerization that allows us to set up an infrastructure within one second with the help of images stored in Docker. Know more here about docker.

In this article, we are going to create a simple machine learning model on top of a container our model will predict a salary based on employee experiences.


1. Linux OS

Install Python on the local system

yum install python36 -y

Configure the repository for docker:

Create a file “/etc/yum.repos.d/docker.repo”

name=docker repo baseurl=

Installing Docker :

yum install docker-ce --nobest -y

Now, let’s start creating a machine learning model on top docker container

Pull Image and run a docker container

Make sure docker is installed in your system using docker — version then start docker and check the status of docker.

Find Docker present or not

docker --version

Start Docker services

systemctl start docker

Check the status of docker services

systemctl status docker

Now, that we have docker service running on our RedHat 8 system. Now, we need to pull the centos image from dockerHub.

To pull any image use syntax

>> docker pull imageName:Tag

Here we are going to use centos latest version

>> docker pull centos:latest

So, now our centos latest version is downloaded using this we can run the container.

To Create a container needs to use the below format

docker run -it --name Cont_Name Image_Name:Tag

Install commands in the container which is required

yum install clear net-tools python36 -y

We need to install some python libraries which we are going to use in the Machine learning code.

  • sklearn
  • pandas
pip3 install pandas scikit-learn
Sklearn and Pandas install

Now, our base environment is ready. Now, we need to get the dataset inside the docker container. The“SalaryData.csv” file to the centos container in the root directory.

Copy local system to the container

docker cp <SOURCEFILE_PATH>  <CONTAINER_NAME>:<DESTINATION_PATH>SOURCEFILE_PATH: Path to the file inside your baseOS i.e here 
RHEL8CONTAINER_NAME: Path of the container name in which you want to transfer file.
Note: Container should be running.
DESTINATION_PATH: Path inside docker container where you wanted to copy the file from baseOS.

From local Linux run below command

docker cp SalaryData.csv 406102f68806:/

Here, we can see dataset is copied to the container directory

Salary dataset

The SalaryData.csv is successfully copied.

Salary prediction model file for creating a model


Creating a Prediction model using the below code

Now, let us check whether it is working well or not. Run the file using the command python3

Model Created

Successfully model is created and saved to salary_model.pkl

Create a code for the prediction of salary

Prediction code

Now, we have a model created. Now, if we want to predict the salary for the experience. Create a file and add below code

Now, we can run this script. It will first load the model and predict the salary based on the years of experience we provided.


Now, we have successfully created a Model and done prediction inside a docker container.

Thanks for Reading !!

Keep Learning !! Keep Sharing !!

You can contact me on😅:

LinkedIn emailme



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store