๐Ÿง Deploying Machine Learning Models inside Docker Containers ๐Ÿณ

Parth Patel
5 min readMay 27, 2021

๐—›๐—ฒ๐—น๐—น๐—ผ ๐—˜๐˜ƒ๐—ฒ๐—ฟ๐˜†๐—ผ๐—ป๐—ฒ, ๐—œ ๐—ฎ๐—บ ๐—ฏ๐—ฎ๐—ฐ๐—ธ ๐˜„๐—ถ๐˜๐—ต ๐˜†๐—ฒ๐˜ ๐—ฎ๐—ป๐—ผ๐˜๐—ต๐—ฒ๐—ฟ ๐˜๐—ฎ๐˜€๐—ธ ๐—ผ๐—ฟ ๐—ฐ๐—ฎ๐—ป ๐˜€๐—ฎ๐˜† ๐—ฝ๐—ฟ๐—ผ๐—ท๐—ฒ๐—ฐ๐˜ ๐—ฎ๐—ฏ๐—ผ๐˜‚๐˜ โ€œ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด ๐—ฎ๐—ป๐—ฑ ๐——๐—ฒ๐—ฝ๐—น๐—ผ๐˜†๐—ถ๐—ป๐—ด ๐— ๐—ฎ๐—ฐ๐—ต๐—ถ๐—ป๐—ฒ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป๐—ถ๐—ป๐—ด ๐— ๐—ผ๐—ฑ๐—ฒ๐—น๐˜€ ๐—ถ๐—ป ๐——๐—ผ๐—ฐ๐—ธ๐—ฒ๐—ฟ ๐—–๐—ผ๐—ป๐˜๐—ฎ๐—ถ๐—ป๐—ฒ๐—ฟ๐—ถ๐˜‡๐—ฎ๐˜๐—ถ๐—ผ๐—ปโ€

To know more about docker and how to setup on RHEL8. You can refer my previous Blog.

๐‹๐š๐ฎ๐ง๐œ๐ก๐ข๐ง๐  ๐€ ๐–๐ž๐›๐ฌ๐ž๐ซ๐ฏ๐ž๐ซ ๐Ž๐ง ๐ƒ๐จ๐œ๐ค๐ž๐ซ ๐‚๐จ๐ง๐ญ๐š๐ข๐ง๐ž๐ซ | by Parth Patel | Medium

Hope you have referred above blog and understood ๐——๐—ผ๐—ฐ๐—ธ๐—ฒ๐—ฟ.

Now Lets Move and understand about ๐Œ๐š๐œ๐ก๐ข๐ง๐ž ๐‹๐ž๐š๐ซ๐ง๐ข๐ง๐ .

Machine Learning :-

Machine learning is a method of data analysis that automates analytical model building. It is a branch of artificial intelligence based on the idea that systems can learn from data, identify patterns and make decisions with minimal human intervention.

In data science, an algorithm is a sequence of statistical processing steps. In machine learning, algorithms are โ€˜trainedโ€™ to find patterns and features in massive amounts of data in order to make decisions and predictions based on new data. The better the algorithm, the more accurate the decisions and predictions will become as it processes more data.

Some more concepts about ๐——๐—ผ๐—ฐ๐—ธ๐—ฒ๐—ฟ.

Dockerfile :-

Each Docker container starts with a Dockerfile. A Dockerfile is a text file written in an easy-to-understand syntax that includes the instructions to build a Docker image . A Dockerfile specifies the operating system that will underlie the container, along with the languages, environmental variables, file locations, network ports, and other components it needs โ€” and, of course, what the container will actually be doing once we run it.

Docker Image :-

Once you have your Dockerfile written, you invoke the Docker build utility to create an image based on that Dockerfile. Whereas the Dockerfile is the set of instructions that tells build how to make the image, a Docker image is a portable file containing the specifications for which software components the container will run and how.

So What is the Task Requirement :-

  1. Pull the Docker container image of CentOS image from DockerHub and create a new container.
  2. Install the Python software on the top of docker container
  3. In Container you need to copy/create machine learning model which you have created in jupyter notebook

Lets do something more here by creating ML ready image using Dockerfile.

Hope you have already installed and started docker service. if not refer above given blog for reference.

First lets create new workspace in system.

Step 1:-

First of all to make Machine learning model we require some Dataset to work on. In this step clone my github repository where i have already provided required dataset.

command :- git clone <URL>

Step 2:-

Now as we have datasets lets create some python Ml code. Create a new file and write below code

Note :- Create this file in the current directory and not in / directory.

In this Code I have just created a Linear Regression Model to predict Salary of employees on the basis of their Experience. So here I have used scikit-learn library and then from sklearn.linera_model I have used LinearRegression function.

Step 3 :-

Now the next step is to create Dockerfile with some docker commands/instructions to create our own new image.

Note :- Create this Dockerfile in /ml directory.

Step 4 :-

We are almost done with everything and Now we are going to Build the image from the Dockerfile using buid command.

command :- docker build -t salaryapp .

Note :- Here โ€˜.โ€™ means current directory as we have our Dockerfile in same folder.

Step 5 :-

After Creating Image we have to run it or launch containers using run command.

command :- docker run -it salaryapp

Note :- -it means interactive terminal i.e we are telling our container to give some terminal to interact

So the app finally looks like this so container automatically starts and run code and give us interactive terminal to interact,

As soon as our program finishes the container automatically stops.

GitHub Link :-

the-helel/MlSummer (github.com)

Hope you understood the concept and performed this task with me.

Thank you for reading my article๐Ÿค—

Keep Learning๐Ÿ’ซKeep Sharing๐Ÿค๐Ÿป

Linkedin Profile :-

Do clap if this article was helpful. Good Day!

--

--

Parth Patel

ARTH Learner | Bigdata โ€” Hadoop | Linux | Front-End Developer | Flask | Coding Enthusiast | Python | AWS | Ansible | Kubernetes