Creating a Machine Learning Model inside Docker Container using Python in RHEL8(Red Hat Enterprise Linux)

Dear Readers,

I would suggest you to first install RHEL-8 on your Virtual Machine/System and Python and then go through this article as we are going to create a Machine Learning Model inside Docker Container in RHEL8.

If you haven’t, you can refer to my blog on how to Install Python in Docker container:

If you haven’t, you can refer to my blog on how to Install Docker in RHEL8:

1.First you need to transfer the data file from your OS to the container.

2. To do so, download WinSCP Application in Windows.

Fig 1. WinSCP Application

3. Type ifconfig enp0s3 in the RHEL8 terminal to find your inet address.

Fig 2. ifconfig enp0s3

4. Copy the IP Address on the RHEL8 terminal and paste it in the WinSCP application and enter your username root along with password to begin a session.

Fig 3. Connecting to Host
Fig 4. Click on Yes to connect

5. Drag and Drop files which are to be transferred.

Fig 5. Drag and Drop files that are to be transferred

6. Check if file is transferred or no. In the RHEL8 terminal, type: cd \root and then ls to check if files have arrived to RHEL8 or no.

Fig 6. Check if file is transferred or no

7. Now, we have to transfer files from RHEL8 to container. To transfer files, type : docker cp /root/<filename> <container_name>:/root/ and then type cd /root/ and type ls. The file should be present inside the container.

Fig 7. File Transfer from RHEL-8 to Docker Container

8. Now we can start with our PYTHON program. So we need to first open editor by typing : vi <filename>.py

Fig 8. Opening Python file editor

9. Press ‘i’ to insert code inside the file and type the code. My example program for explanation purpose is a Salary Prediction Program.
I have used pandas and sklearn packages inside my program.

Figure 9. Salary Prediction Program

10. We first import the libraries. Then, we import our dataset and store it in some object. Then, we store the independent data in ‘x’ and dependent data in ‘y’. We then use Simple Linear Regression(SLR) algorithm for prediction purpose in this program. We then use .fit(x,y) to fit the dependent and the independent data inside the SLR object for training the model and then using .predict(), the machine predicts the best possible output after internal calculations. We finally save or code using :wq.

Fig 10. Saving the program

11. We finally run the program using : python3 <file_name.>.py and see if we have the desired output or if there is any error in the code.

Fig 11. Output of a ML Model Python Program in Docker

Using the above steps, we just trained a model using the .fit(x,y) function and predicted the output. The program also shows the coefficient value and the predicted value, which is very close to the actual output, which shows that our model is trained properly and is predicting an output which is more or less same as the original output.

MLOps Enthusiast and an Avid Learner