Tutorial 10 :Deploying ML model on Heroku platform using Docker

Gaurav Patil
4 min readOct 30, 2022
Photo by Rubaitul Azad on Unsplash

Missed previous tutorial ? : click here.

Prerequisites :

  1. Refer step 1 of this tutorial for signing-up and creating an application on Heroku. Note down application name since we will be using it in the Dockerfile. I created an application by the name song-popularity-model-docker.
  2. You must have Github account too.
  3. Git basic commands.

We will cover following topics in this tutorial :

Creating the Dockerfile

Building the Docker image

Listing the Docker images

Building the Docker container

Adding Git remote

Pushing all the files to Heroku using Git

Step 1 ) Creating the Dockerfile :

You can refer to my gitub repository or my Heroku deployment tutorial.

Create an empty folder. In this folder, add all the files from above repository. Also you must have a Dockerfile as given below in this folder.

Write your email-id, Github account username and Heroku application name(See prerequisite no.1) in line numbers 8, 9, and 11 respectively in the Dockerfile given below :

FROM python:3.9.14RUN apt-get update && apt-get install -y --no-install-recommends \
nano \
git \
curl
RUN curl https://cli-assets.heroku.com/install-ubuntu.sh | shRUN mkdir /app
WORKDIR /app
COPY main.py model.pkl wsgi.py Procfileruntime.txt requirements.txt ./
COPY templates ./templates
RUN git config --global user.email <"your-email.com">
RUN git config --global user.name <"Github-account-username">
RUN git init
RUN heroku git:remote -a <Heroku-application-name>
RUN git add .
RUN git commit -am "first commit message"

Step 2) Building the Docker image :

Run following command in the terminal to build the Docker image :

docker build -t heroku_image:v1 .

Output :

You can see the output saying Docker image is built successfully.

Step 3) Listing the Docker images :

Run following command to enlist all the Docker images built :

docker images

Output:

You can see the output which shows the image name, ID and size.

Step 4) Building the Docker container :

Run following command in the terminal to run the Docker container in an interactive mode :

docker run -it --name heroku_container1 heroku_image:v1 /bin/bash

Output :

Then run the command : heroku login while Docker container is in interactive mode.

Click on the link that appears in the output, you will be redirected to the Heroku login page. Click on “Log in” button.

After logging in, go to the terminal, following message will pop-up on the screen :

Step 5) Adding Git remote :

git:remote -a <heroku-app-name>

Run following Git command in the terminal while Docker container is in interactive mode :

git:remote -a song-popularity-model-docker

Output :

Step 6) Pushing all the files to Heroku using Git :

Run following Git command in the terminal while Docker container is in interactive mode :

git push heroku master

Output :

Click on the link thus obtained (underlined with yellow color in above image), you will be directed in the browser which will show form as given below. Fill some random values for the features and hit the submit button.

You can check the output on the same page (“This song is not popular.”) as shown below :

Congratulations !!

You have completed the Docker tutorial series !!

After completing all previous tutorials, you have gained pretty good understanding of Docker.

Thank you !!

--

--

Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.