Deploy Python/Django app as Docker container on fly.io's Public Cloud

Author: aashutosh

Updated: 9 Mar 2023 12:41 p.m.

Tags: #Python #Django #Docker #Cloud #fly.io

Summary : Deploy a full stack Python Django application as a Docker container image on fly.io Platform as a Service (PaaS) Public Cloud . Connect a Postgres Database running on Fly.io to Django app.

Fly Cloud transforms docker container images into and run as Firecracker (open source virtualization technology) micro-VM on bare metal servers across Data centers.

 

Step 1:  Install flyctl
#Linux
curl -L https://fly.io/install.sh | sh

#Mac
brew install flyctl

#Windows
powershell -Command "iwr https://fly.io/install.ps1 -useb | iex"

 

Step 2:  Change directory to Django project
cd django-project-directory

 

Step 3:  Copy Python Package dependency list to requirements.txt
pip freeze > requirements.txt

 

Step 4:    Configure Django app with flyctl launch

fly launch

>fly launch
Update available 0.0.442 -> v0.0.472.
Run "fly.exe version update" to upgrade.
Creating app in C:\Softwares\django\app\aashutosh
Scanning source code
Detected a Django app
? Overwrite "C:\Softwares\django\app\aashutosh\.dockerignore"? Yes
? Overwrite "C:\Softwares\django\app\aashutosh\Dockerfile"? No
? Choose an app name (leave blank to generate one): aashutosh
automatically selected personal organization: Aashutosh Kumar
? Choose a region for deployment: Hong Kong, Hong Kong (hkg)
Created app quasartech in organization personal
Admin URL: https://fly.io/apps/aashutosh
Hostname: aashutosh.fly.dev
Set secrets on aashutosh: SECRET_KEY
Creating database migrations
Wrote config file fly.toml
? Would you like to set up a Postgresql database now? No
? Would you like to set up an Upstash Redis database now? No
Your Django app is almost ready to deploy!
We recommend using the database_url(pip install dj-database-url) to parse the DATABASE_URL from os.environ['DATABASE_URL']
For detailed documentation, see https://fly.dev/docs/django/

 

fly launch scan & detect source code automatically as a Djnago app and create two files with settings configured for deploying Djangi application: 

  1. fly.toml  : contains configuration details like below to run as Django app on container/Micro VM, modify it suit your needs, but it comes with default setting required to deploy Django app. 

  1. Dockerfile

 

 
Step 5: Deploy App
flyctl deploy

fly deploy will build the image from Dockerfile and push it on Fly.io

Deploy Django app using fly deploy, this command will build the image using information/steps from Dockerfile, and will push the image to Fly.io and run the application using settings from fly.toml

Once deployment is successful, Django app can be accessed online

 
Step 6: Open App

Browse to https://your-app-name.fly.dev/  (https://aashutosh.fly.dev/)
use fly open command to launch browser to https://your-app-name.fly.dev/ 

fly open
 
Step 7: Attach a Postgres Database to Django application on Fly.io
flyctl postgres attach --app <your-app-name> <postgres-db-name>
flyctl postgres attach --app aashutosh aashu-postgres

 

Step 8: ssh into Fly app instance console
flyctl ssh console