Building and deploying a machine learning model in a Docker container
To perform the following tasks -
- Pull the Docker container image of CentOS image from Docker Hub and create a new container
- Install the Python software on the top of docker container
- In the container copy/create a machine learning model
Before performing the tasks let’s understand some basics :
What is Docker?
Docker is an open-source containerization platform that is used for building, deploying, and running applications.
Docker can be understood as a ship carrying containers which consist of applications and all their dependencies, refer the above picture for visualization.
It helps developers to simplify and efficiently accelerate the application workflow along with giving them the liberty to develop using their own choice of technology and development environments.
Applications of Docker -
- Fast, consistent delivery of applications
- Responsive deployment and scaling
- Running more workloads on the same hardware
Now let’s look into our requirements……
Tools used to perform the tasks-
OS : RHEL8 / any other OS
Language : Python
Containerization technology : Docker
- Docker should be installed on the OS. Reference for installation.
- To create a machine learning model, we will require the relevant dataset for that model. The one used to perform the tasks can be downloaded from here.
You can build and deploy any machine learning model inside the container, here for the tasks I have used a simple linear regression model to predict the salary based on years of experience a person has.
A little sneak peek into the dataset based on which the machine learning model is to be built and deployed in the Docker container :
As it can be observed the dataset in our consideration has two columns ‘YearsExperience’ and ‘Salary’ where the former is the independent variable and latter is the target variable.
Now what is simple linear regression algorithm?
To put it briefly simple linear regression is a supervised machine learning algorithm where the predicted output is continuous and has a constant slope. Here, there is only one feature and one target variable. It is used to predict values within a continuous range, (e.g. sales, price) rather than trying to classify them into categories (e.g. cat, dog). It uses traditional slope-intercept form, where ‘m’ and ‘b’ are the variables our algorithm will try to “learn” to produce the most accurate predictions. ‘x’ represents our input data and ‘y’ represents our prediction.
Let’s get to performing the tasks!
To check that you have installed docker on your OS , you can run the following command:
To ensure that docker starts automatically when OS boots, the command given below can be used.
systemctl enable docker
1. Pull the Docker container image of CentOS image from Docker Hub and create a new containers
2. Install the Python software on the top of docker container
Launching the docker container:
Installing python3 on it:
Installing other libraries:
3. In Container copy/create the machine learning model
- Make sure the dataset file exists on the OS.
- Make a folder inside the docker container using the following command:
- Use the following command to copy the dataset file into the docker container from the OS command line :
docker cp <source_path> <container_name or container_id>:<destination_path>
- In the above command the destination path specifies the path of the folder created in the docker container and the source path specifies the path in the OS where the dataset file exists.
- Download and install vim editor inside docker container using the following command:
yum install vim
- Change the directory in the docker container and to get inside the folder where the dataset exists using the following command:
- Creating a python file using vim editor to build the machine learning model inside the folder :
- Now we will create another python file using the same command to predict the salary based on the above model that we created.
- Run the files created above to actually predict the salary using the command given below:
By following the above steps we can hence successfully execute the given tasks and subsequently build and deploy the machine learning model inside the docker container.