Creating a Machine Learning Model over Container
Target:
👉 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.

Now, first we require to install and start docker…
To do that we can take help of some tool like Ansible [Click here to learn more].
Once our docker is ready to be worked upon, we can begin with our task…
Step1: Pulling the required image.
To pull any image from DockerHub we only require 3 things:
1. Internet connection.
2. Name of the image.
3. Docker Command.
Once all the requirements are fulfilled you can run the command: docker pull <name of the image>:<tag>
We can run the command docker image ls
to list the pulled images.
If we do not mention any tag for the image to be pulled, docker will pull the image with tag ‘latest’
Step2: Create a container.
After pulling the required image, we can create as many containers from the image as we want. To create a container from the image we can run the command: docker run <name of the image>:<tag>
This command will create a container from the required image, now one would also like to use the container, say via its command line, and to do so we can use the options -t
to get the command line terminal of the container and -i
to make the terminal interactable. So for example we want to run an interactive container made by the image centos:latest, and so the command for this would be… docker run -it centos:latest

Using --name <Name of the container>
we can specify a name for our container if we like.
Step3: Once the container has been launched, we can proceed to configure it in the required way. In our case we require to install Python. To install any software inside a container yum(for centos) comes preconfigured, and hence we are just required to run a simple command to install the software. In our case the we will run the command: yum install python3 -y
Step4: After installing Python we can create our ML model over the required dataset. But first we require to copy the dataset from the base OS to the container. To do that, first get out of the container’s terminal using the command exit
, once we are in our base OS’s terminal we can use the command: docker cp <path to dataset> <name of the container>:<path of the location where we would like to store the dataset>
for example:

This would copy ‘SalaryData.csv’ located in the present working directory to /root/ of our container named ‘test’.
Step5: Now we have imported our dataset to the container, hence we can start to create our model. To create the model, we would require to install some python libraries. To install Python libraries, we can use the ‘pip3’ command. The command to be used are: pip3 install sklearn
pip3 install pandas
pip3 install numpy
Step6: Once all the previous steps are complete we can proceed to write the code our model that we want to train.

This code will import the dataset, train the model and then dump the model in a .pk1 file. It will also print somethings about our model like the coefficient, y-intercept, prediction at x=1.1 and lastly the accuracy of our at point 1.1.
To, run the code use the command python3 <name of code's file>

Here I used the ls command at the end to check whether the required dump was created or not. The test run seems good…
That’s it, task completed successfully👍
Thank you for reading, you rock🙌✨