Whipping Up Flask and PostgreSQL Magic with Dokku PAAS
Well, well, well! My grand date night plans just got a plot twist – she cancelled the plan. Classic move! But who needs romance when you can have a thrilling adventure in the world of DevOps, right?
So, here we are, back in the magical kitchen where, instead of candlelight dinners, we're about to cook up a storm – deploying a Flask app with a PostgreSQL database using the mystical Dokku PAAS. Nothing says heartbreak recovery like a delightful deployment experience, right?
Welcome, dear readers, to the enchanted world of tech where servers are your only companions, and error messages are your love letters. Who needs a date when you can have a server that never ghosts you? Let's dive into the whimsical world of DevOps, where the only heartbreak is a broken build. Bon appétit, my resilient comrades!
Ingredients:
1 Virtual Private Server (VPS)
1 terminal
1 ssh connection
1 pinch of patience
Recipe:
Step 1: Prepare Your Flask App
In the mystical realm of Flask, ensure your app is configured to use PostgreSQL as its database. Update your config.py
or equivalent file to include the database URI:
import os
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'postgresql://your-db-username:your-db-password@your-db-host:5432/your-db-name'
Unveiling the Procfile Magic
Crafting the Spell:
Open the Realm: Wield your favorite text editor, the wand of code sorcery.
New Script: Create a new file in the root directory and name it
Procfile
(with a capital 'P').Spell Casting: Inscribe the magical incantation:
web: gunicorn notsopink:app
Replace
notsopink
with your Flask app's mystical module.
Unleashing the Magic:
The Procfile is your key to Dokku's heart, guiding the deployment dance. As you push your code, this sacred script ensures your Flask app's graceful performance on the Dokku stage.
May your deployments be magical, and your code forever enchanting!
Step 2: Deploy Dokku
Now, let's sprinkle some Dokku magic on your VPS. Connect to your server via SSH and cast the installation spell. Read More
Step 3: Create a PostgreSQL Database
Summon a PostgreSQL database for your app:
# on 0.19.x+
sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres
# You can also specify custom environment variables to start the postgres service in semicolon-separated form.
export POSTGRES_CUSTOM_ENV="USER=alpha;HOST=beta"
dokku postgres:create pinkcat
# to print info about the db service
dokku postgres:info pinkcat
# list all postgres services
# usage
dokku postgres:list
# get logs?
dokku postgres:logs pinkcat --tail
Forge a magical link between your database and app:
dokku postgres:link pinkcat notsopink
Step 4: Set Up Environment Variables
Infuse your Flask app with power by setting environment variables:
dokku config:set notsopink FLASK_APP=your-app-entry-file.py
dokku config:set notsopink FLASK_ENV=production
dokku config:set notsopink SECRET_KEY=your-secret-key
dokku config:set notsopink DATABASE_URL=postgres://your-db-username:your-db-password@your-dokku-host/your-db-name
Step 5: Deploy Your Flask App
Forge a connection between your local Git repository and the Dokku realm:
git remote add dokku dokku@your-dokku-host:your-app-name
Initiate the grand deployment ceremony:
git push dokku master
Step 6: Access Your Flask App
Embark on a journey http://your-app-name.your-dokku-host
in your browser. Behold! Your Flask app has come to life!
And there you have it – a recipe for success served with a side of humor! Bon appétit!
Subscribe to my newsletter
Read articles from Harsh Vardhan Goswami directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Harsh Vardhan Goswami
Harsh Vardhan Goswami
Full stack developer with a sense of humor, always striving to turn code into laughs. Building awesome and making people smile, one line at a time.