Deploying a Dockerized Flask App to Heroku
Tags: docker, python, flask, heroku
Published: 2020-06-15 06:43:49

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o0m7qye387sujfvge108.jpeg)

Photo by [Andy Holmes](https://unsplash.com/photos/webyw4NsFPg?utm_source=dropbox_paper&utm_medium=referral)

Welcome! This tutorial is going to be divided into 3 parts.

    * Part 1 - Create A Basic Flask App
    * Part 2 - Dockerize the Application
    * Part 3 - Deploy the Application to Heroku

Prerequisites:
    
    - Git/Github - https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
    - Flask - https://flask.palletsprojects.com/en/1.1.x/installation/
    - Heroku - https://devcenter.heroku.com/articles/heroku-cli#download-and-install
    - Your Fav Code Editor (VSCode or Atom)


## Part 1 - Create A Basic Flask App

Our Flask App is going to be a basic “Hello World!” app. 

Firstly, create the directory for our project

    $ mkdir Flask-Docker-App

    $ cd Flask-Docker-App

Then, we’d initialize Git

    $ git init

Open your code editor and create 4 files.

    - app.py - Our main script
    - .gitignore - A file that tells Git to ignore some files within your project
    - README.md - The ReadMe file for your project.
    - requirements.txt - This specifies all the packages used in your project. 

Set up a virtual environment to use for our application:
```
$ python3 -m venv env
$ source env/bin/activate
```
When Flask is installed(see link above), it downloads other packages in order to make it work efficiently. In order to add these packages to our “requirements.txt” file. We do

    $ python -m pip freeze > requirements.txt

Then, we’d add the following code to app.py

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello():
      return "Hello World!"
    
    if __name__ == '__main__':
      app.run()

Then, we’d run app.py

    $ python app.py

The Flask App would be hosted on http://localhost:5000/.  End the server by pressing “Ctrl + C”.

Congratulations! You’ve made your first “Hello World!” Flask App.

## Part 2 - Dockerize the Application

For this section, you’d need Docker installed.

On your code editor, create a “Dockerfile”(no extensions)

Then, edit your “Dockerfile”

    FROM python:3.8.1
    
    ENV APP_HOME /app
    WORKDIR $APP_HOME
    
    COPY . /app
    
    RUN pip install -r requirements.txt
    
    ENTRYPOINT ["python app.py"]
    

Before building your image, your app.py file needs to be edited

    if __name__ == '__main__':
      app.run(debug = True, host = '0.0.0.0')

Then, Build the docker image

    $ docker build -t <your username>/flask-docker .

And run it

    $ docker run -it -p 2000:5000 <your username>/flask-docker

This allows you to test your app locally. It’s hosted on http://localhost:2000. 

End the server by pressing “Ctrl + C”.


## Part 3 - Deploy the Application to Heroku

Heroku needs to be installed (see link above). Create an heroku account, then run

    $ heroku login

Once logged in, open your code editor and create an “heroku.yml” file. Then add this to the file

    build:
      docker:
        web: Dockerfile

If your “Dockerfile” is within another folder

    build:
      docker:
        web: <some-folder>/Dockerfile

Next, we create our heroku app

    heroku create <app-name>

And add the remote branches

    git remote add <branch-name> <app-link>

Heroku runs your app depending on the ports available. In several instances, It might not assign the port 5000 which means our app crashes. In order to fix this error, we’d add this to our “app.py”

    import os
    
    # ----Remainder of Code----
    
    if __name__ == '__main__':
      port = int(os.environ.get('PORT', 5000))
      app.run(host = '0.0.0.0', port = port)

This tells our app to use the port assigned by heroku, if not, use 5000.

Then, we’d need to commit changes made so far. This is done by:

    git add .


    git commit -m "First Commit"

But, heroku needs to know your app is ran within a container. So, we type

    $ heroku stack:set container

Finally, we’d push our changes

    $ git push <branch-name> master


Conclusion

Congratulations! You’ve made it to the end of this tutorial. I hope you learnt some new things about Docker and Heroku.