Machine Learning Code on Docker


Docker developed by Solomon Hykes in 2013, is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.

It allows us to launch (install, boot and login) the containers just in 1 or 2 seconds as compared to 15–30 minutes in setting up virtual machines or operating systems. This fast set up is very useful for developers.

Docker provides isolated environments which is very consistent. It has decreased deployment time to seconds which speeds up the development process tremendously.

Here in this article, we are going to set up docker and create machine learning code over there. So let’s begin :

  1. Creating yum repo for docker : First of all to install docker in our RHEL8 system, we have to create a separate repository in /etc/yum.repos.d folder, with .repo extension. Here I’ve given name docker.repo

Inside the repo, we define a URL where yum can find the docker software compatible with our system.

2. To check if repo is correctly configured : # yum repolist

3. Install docker : We are going to install community version of docker as it is free and open source.

We use ‘ — nobest’ to install best version available.

4. Start docker services : Start the services of docker (1 time cmd) then check the status of service, it should say “active (running)”.

If docker has started successfully, try running # docker info command. It gives lot of information about docker in our system.

5. Pulling centos image: Currently we have no docker image in our system.

Generally we use a public repository called to get docker images. Multiple versions of these images are available called Tags. To pull centos image :

We can see centos image shows up in our docker images.

6. Creating container : To see how many containers are running -

Currently no container is running in our base OS. To launch a container :

In the above command, run launches a new container with centos image. Option -i is used to make terminal interactive i.e. it keeps STDIN open. Option -t allocates a terminal to the container. ‘ML1' is the name we have given to the container. It is not compulsory. If we do not give name to container, docker will provide some random name to it along with container ID.

7. Installing python in the container : Before installing python in container, we need to make few changes in the firewall of BaseOS if it is facing trouble in ping and installing softwares.

▪ Masquerading allows for docker ingress and egress: # firewall-cmd — zone=public — add-masquerade — permanent

▪ To specifically allow incoming traffic on ports 80(HTTP) and 443(HTTPS) : #firewall-cmd — zone=public — add-port=80/tcp

# firewall-cmd — zone=public — add-port=443/tcp

▪ Reload the firewall : # firewall-cmd — reload

▪ Restart the docker services : # systemctl restart docker

Now install python3 in the container :

8. Creating Machine Learning Model: Firstly we transfer dataset from our host to the container : # docker cp [Source File] [Container]:[Path]

To run ML code in container, we require python libraries like scikit-learn, pandas, numpy & joblib. Install them using pip command.

# pip3 install [library name]

Now we run our Simple Linear Regression code to create model.

As we can see on running # python3 , a new file name PredictSalary.pk1 is created. This contains our model. Now we can use our model anywhere without training it every time just by loading this file. file is used to predict the salary.

So, this way we have successfully run Machine Learning code in Docker container.

For complete code and data file, do visit my github profile.

Thank You for reading. Your feedbacks are welcomed.

Reach to me at :