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.
#Linux
curl -L https://fly.io/install.sh | sh
#Mac
brew install flyctl
#Windows
powershell -Command "iwr https://fly.io/install.ps1 -useb | iex"
cd django-project-directory
pip freeze > requirements.txt
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:
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.
app = "aashutosh"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []
[deploy]
release_command = "python manage.py migrate"
[env]
PORT = "8000"
[experimental]
auto_rollback = true
[[services]]
http_checks = []
internal_port = 8000
processes = ["app"]
protocol = "tcp"
script_checks = []
[services.concurrency]
hard_limit = 25
soft_limit = 20
type = "connections"
[[services.ports]]
force_https = true
handlers = ["http"]
port = 80
[[services.ports]]
handlers = ["tls", "http"]
port = 443
[[services.tcp_checks]]
grace_period = "1s"
interval = "15s"
restart_limit = 0
timeout = "2s"
[[statics]]
guest_path = "/app/public"
url_prefix = "/static/"
# replace demo.wsgi with <project_name>.wsgi
CMD ["gunicorn", "--bind", ":8000", "--workers", "2", "core.wsgi"]
ARG PYTHON_VERSION=3.10-slim-buster
FROM python:${PYTHON_VERSION}
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /code
WORKDIR /code
COPY requirements.txt /tmp/requirements.txt
RUN apt-get update
# Install psycopg2 dependency - libpq-dev, gcc
RUN apt-get -y install libpq-dev gcc
RUN set -ex && \
pip install --upgrade pip && \
pip install -r /tmp/requirements.txt && \
rm -rf /root/.cache/
# Copy Local Source code to image:/code
COPY . /code/
# Copy certificate files
# RUN mkdir ~/.postgresql/root.crt
# COPY cockroach_db/root.crt ~/.postgresql/root.crt
RUN python manage.py collectstatic --noinput
EXPOSE 8000
# replace demo.wsgi with <project_name>.wsgi
CMD ["gunicorn", "--bind", ":8000", "--workers", "2", "core.wsgi"
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
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
flyctl postgres attach --app <your-app-name> <postgres-db-name>
flyctl postgres attach --app aashutosh aashu-postgres
flyctl ssh console