DEV Community

Cover image for How to deploy a Flask app to Digital Ocean's app platform
Amit Jotwani
Amit Jotwani

Posted on

How to deploy a Flask app to Digital Ocean's app platform

I recently went through the process to put my side project howbigisthebaby.io on Digital Ocean's new app platform, and wanted to document it in case anyone else found it helpful (or atleast me revisiting this later).


Part 1: Setup the environment for the Flask app

Step 1: Create the virtual environment on your machine

$ virtualenv venv -p Python3
Enter fullscreen mode Exit fullscreen mode

Step 2: Activate the virtual environment

$ source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Flask and gunicorn

$ pip install Flask gunicorn
Enter fullscreen mode Exit fullscreen mode

Step 4: Freeze the requirements in requirements.txt file

Now that you have the flask package installed, you will need to save this requirement and its dependencies so App Platform can install them later. Do this now using pip and then saving the information to a requirements.txt file:

$ pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Step 5: Write code for your Flask app in app.py. Here's a simple "hello world" app

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'
Enter fullscreen mode Exit fullscreen mode

Part 2: Setting Up Your Gunicorn Configuration

Since we cannot use Flask's built-in server in production environment, we will use Gunicron web server Why Nginx/Gunicorn/Flask?

While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well and by default serves only one request at a time.
Source: Flask's docs

Step 1: Create the gunicorn config file

$ touch gunicorn_config.py
Enter fullscreen mode Exit fullscreen mode

Step 2: Add this configuration to gunicorn_config.py file

# inside gunicorn_config.py
bind = "0.0.0.0:8080"
workers = 2
Enter fullscreen mode Exit fullscreen mode

Part 3: Pushing the Site to GitHub

The way the Digital Ocean app platform works is that it syncs with a GitHub repo. Any changes to this GitHub repo will automatically be pushed to your app living on Digital Ocean's app platform.

Step 1: Create a new repo on GitHub.com
Open your browser and navigate to GitHub, log in with your profile, and create a new repository called flask-app.

Step 2: Then, create a .gitignore file, and add .pyc so these files are not pushed to GitHub.

$ git init
$ nano .gitignore
Enter fullscreen mode Exit fullscreen mode
# inside .gitignore
.pyc
Enter fullscreen mode Exit fullscreen mode

Step 3: Add files to your repository, and commit the changes

$ git add app.py gunicorn_config.py requirements.txt .gitignore
$ git commit -m "Initial Flask App"
Enter fullscreen mode Exit fullscreen mode

Step 4: Add the GitHub repo you created earlier as the remote repository

$ git remote add origin https://github.com/your_username/flask-app
Enter fullscreen mode Exit fullscreen mode

example: git remote add origin https://github.com/ajot/hbb-flask-webhook

Step 5: Push to GitHub

$ git branch -M main
$ git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Part 4: Deploying to Digital Ocean's app platform

  1. Create a new "app" on Digital Ocean.
  2. Choose GitHub as source
  3. Choose the GitHub repo you just created
  4. Follow the promots to create the app
  5. Edit the run command to this - gunicorn --worker-tmp-dir /dev/shm app:app

Bonus Tip: You can also set environment variables in Digital Ocean, and then access them in your code like this -

airtable_api_key = os.environ.get('airtable_api_key')
airtable_base = os.environ.get('airtable_base')
Enter fullscreen mode Exit fullscreen mode

Resources:
How To Deploy a Flask App Using Gunicorn to App Platform | DigitalOcean

Top comments (1)

Collapse
 
heyaustinwood profile image
Austin Heywood

if you are having trouble setting up and getting flask with postgres to run on digitalocean app platform, i created a simple project with instructions to get a flask project running on digitalocean quickly here:

github.com/heyaustinwood/flask-do-...