Show Off Your Machine Learning Project on Web Part 3: Deploy onto Heroku with Docker
What client needs are a domain name and a browser.
Visit part 1 and part 2 by clicking them.
Click this to go through paywall.TL;DR: You can get the example project here.
Motivation
After you create a dockerized environment, you think you should deploy your app on cloud so that you can show off your machine learning project, web development and DevOps skills. In addition, others can access your project with just one click on URL you provide without the knowledge of setting up the environment.
Goal of This Part
This part will teach you how to deploy your dockerized app onto Heroku, and tell you some specialized settings of Heroku when deploying with Docker.
Before Deploy to Heroku
Heroku provides three ways to deploy your dockerized app:
- Container Registry & Runtime (Docker Deploys)
- Building Docker Images with heroku.yml
- Local Development with Docker Compose
As I don’t need Docker components for my app (only one streamlit service), there is no need to use Docker Compose.
Thought Most of posts use Method #1, I will choose Method #2 for these two reasons:
- Rarely the posts mention about this method.
- I write a specialized Dockerfile for Heroku deployment.
The Components We Need
For Method #2, we need at least two files:
Dockerfile
heroku.yml
: to indicate Heroku what you want for building and deploying.
In my case, I need three files because I like modularization:
Dockerfile
heroku.yml
- heroku_startup.sh
Dockerfile
Just like part 2 with some changes for Heroku deployment:
You can see the changes on line 42 and 48. The reasons are that:
- Change the permission of
heroku_startup.sh
or you will receivepermission denied
error while executingheroku_startup.sh
. ENTRYPOINT "./heroku_startup.sh"
to tell Docker to executeheroku_startup.sh
when starting the container.
Heroku.yml
Heroku needs heroku.yml
to build and deploy Docker Images, so I create one for my usage:
build:
docker:
web: heroku.Dockerfile
run:
web: ./heroku_startup.sh
Obviously, the build
and run
sections indicate what you want to do in build and run stage on Heroku, respectively.
heroku_startup.sh
Mentioned here, Heroku uses a$PORT
environment variable for port exposure. So the heroku_startup.sh
will look like this:
#!/bin/bash
echo PORT $PORT
streamlit run --server.port $PORT app.py
You have to set the $PORT
variable to streamlit or your app will not appear.
Deploy!
This step is rather easy, what you need is to deploy with Git. Then Heroku will use Heroku.yml
rather Procfile
to deploy your app with Docker.
Recap
This part shows how to deploy your machine learning app onto Heroku and mention some special setting on Heroku.
I hope this series can help you to combine your machine learning app with web and DevOps development cycle so that you can catch up the trend and won’t be mocked by web development company!
Originally published at https://cuda-chen.github.io on June 29, 2020.
If you have any thoughts and questions to share, please contact me at clh960524[at]gmail.com. Also, you can check my GitHub repositories for other works. If you are, like me passionate about machine learning, image processing and parallel computing, feel free to add me on LinkedIn.