How to deploy Python Flask web-apps to Heroku?

Shashank Srivastava
4 min readApr 1, 2020

--

Learn how you can quickly deploy your Python Flask web-apps to Heroku in easy steps.

My Flask web-app running on Heroku
https://india-covid-stats.herokuapp.com — My Flask web-app is deployed on Heroku.

In my last post, I explained how you can create a Python Flask web-app that scrapes data from the Ministry of Health & Family Welfare & display that using Chart.js. I have deployed my app to Heroku as well so that you can always access it using your browser.

You might also want to deploy your own Flask web-apps to Heroku. Heroku’s free tier allows you to deploy up to 5 applications. You can see your app live there in a matter of minutes if not seconds.

And it is easier than you might think! In this post, I will show you how you can do it.

Please note that this post is based on a Flask application that doesn’t need any database.

Requirements

  1. A free Heroku account.
  2. Heroku CLI installed on your workstation.
  3. A running Flask web-app that you want to deploy.
  4. Docker installed & running on your workstation.

Before I start this tutorial, a word or two about why I am Dockerising my web-app instead of deploying it directly. The reason is portability & ease of use. By converting our app to a Docker container, we are making the deployment process easy & convenient. Heroku itself will take care of managing the containers. Also, doesn’t it provide a way to learn Docker a bit as well? :-)

With that said, let’s get started.

Steps to perform

  1. First, create an account on Heroku if you haven’t done it already. Go to https://www.heroku.com/pricing and select the Free tier.
  2. Download & install the Heroku CLI on your machine. For this, go to https://devcenter.heroku.com/articles/heroku-cli & choose the installer as per your OS. Make sure you can invoke the heroku command from your terminal.
  3. Create a Dockerfile. For this, you can clone my GitHub repository (linked below) or use your own application’s Dockerfile. Make sure that Dockerfile is placed inside your project’s root.

My Dockerfile looks like this.

FROM python:3.7-alpine
COPY requirements.txt /src/requirements.txt
RUN pip install --upgrade pip
WORKDIR /src
RUN pip install -r /src/requirements.txt
COPY app.py /src
COPY static /src/static
COPY templates /src/templates
CMD python /src/app.py

4. Create a Heroku application. For this, execute the below command.

$ heroku create your_app_name

Note the app name here. This is the name you want to give to your application. If you don’t specify a name, then Heroku will generate a random (and funny) name for you.

Once Heroku is done creating your application, it will show you the URL of your web-app. You can note it down somewhere or the URL is in the below format.

https://your_app_name.herokuapp.com

5. Login to Heroku Container Registry.

Logging on to Heroku Container Registry means you’re telling Heroku where to push your Docker image to. It runs it’s registry at registry.heroku.com.

So, enter the below command. It will open a browser window where you can enter your Heroku credentials. Once you enter the correct credentials, you can close the browser window.

$ heroku container:login

6. Build & publish your Docker image.

Enter the below command. It will create a new Docker image & then push it to Heroku Container Registry.

$ heroku container:push web --app your_app_name

Replace your_app_name with the name of the application you chose in step #4.

This step will take some time depending upon how big your image is.

7. Create a new release of your application.

Creating a new release will deploy your application to Heroku & expose it to the public so you can access it via a browser.

So, all you need is to enter the below command & your application will be live.

$ heroku container:release web --app your_app_name

Again, please pay attention to the application name. You need to use your own application’s name here.

Once you have created a release, you can enter the URL in the browser or execute the below command to open the URL automatically.

$ heroku open --app your_app_name

And that’s all for this post. In the next post, I will show you how you can automatically deploy your Flask web-app to Heroku after pushing your commits to your GitHub repository.

See you then! Take care & stay safe!

--

--

Shashank Srivastava
Shashank Srivastava

Written by Shashank Srivastava

DevSecOps Architect @Virtualness. Music/Book/Photography/Fitness lover & Blogger.

No responses yet