How to Deploy Django Applications to DigitalOcean Using Nanobox

15 September 2017

Django, for many Python developers, is the go-to web framework. It's known for its clean design and facilitation of rapid development cycles. DigitalOcean is a rising star in the hosted VPS world, offering high-performance SSD cloud servers (which they lovingly call "droplets"), a dead-simple API, and a clean user interface.

In this article, I'm going to walk through deploying a Django application to DigitalOcean using Nanobox. Nanobox uses Docker to provision local development environments, local/remote staging environments, and scalable, highly-available production environments on DigitalOcean.

Start the Local Dev Environment

With the boxfile.yml in place, you can fire up a virtualized local development environment. I recommend adding a DNS alias just so the app will be easier to access from a browser.

# Add a convenient way to access the app from a browser
nanobox dns add local django.local
# Start the dev environment
nanobox run

Nanobox will provision a local development environment, spin up a containerized Postgres database, mount your local codebase into the VM, load your app's dependencies, then drop you into a console inside the VM.

Generate a New Django Project

If you have an existing Django project, you can skip this section. To generate a new Django project from scratch, run the following from inside the Nanobox console:

Run Data Migrations

Run a data migration for any remaining INSTALLED_APPS. You’ll need to decide which apps you want enabled by default. You can disable apps by commenting them out in the INSTALLED_APPS section of app/settings.py file. Unless you commented out all of the INSTALLED_APPS, you’ll need to run any pending data migrations:

nanobox run python manage.py migrate

Run Django Locally

With your ALLOWED_HOSTS and database connection updated, you're ready to start Django in your local dev environment. When running web apps inside Nanobox, they should broadcast on 0.0.0.0. From the /app directory in your Nanobox console:

Name your provider and choose a default region. The name is arbitrary and only meant to help you identify it in your list of provider accounts.

Launch a New App

Go to the home page of your Nanobox dashboard and click the "Launch New App" button. Select your DigitalOcean provider from the dropdown and choose the region in which you'd like to deploy your app.

Confirm and click "Let's Go!" Nanobox will order a droplet on DigitalOcean under your account. When the droplet is up, Nanobox will provision platform components necessary for your app to run:

Load-Balancer: The public endpoint for your application. Routes and load-balances requests to web nodes.

Monitor: Monitors the health of your server(s) and application components.

Logger: Streams and stores your app's aggregated log stream.

Message Bus: Sends app information to the Nanobox dashboard.

Warehouse: Storage used for deploy packages, backups, etc.

Once all the platform components are provisioned and running, you're ready to deploy your app.

Stage Your App Locally

Nanobox provides "dry-run" functionality that simulates a full production deploy on your local machine. This step is optional, but recommended. If the app deploys successfully in a dry-run environment, then it will work when deployed to your live environment.

Deploy

Add Your New App as a Remote

From the root of your project directory, add your newly created app as a remote.

nanobox remote add app-name

This connects your local codebase to your live app. More information about theremotecommand is available in the Nanobox Documentation.

Deploy to Your Live App

With your app added as a remote, you're ready to deploy.

nanobox deploy

Nanobox will compile and package your application code, send it up to your live app, provision all your app's components inside your live droplet, network everything together, and voila! Your app will be live on DigitalOcean.

Manage & Scale

Once your app is deployed, Nanobox makes it easy to manage and scale your production infrastructure. In your Nanobox dashboard you'll find health metrics for all your app's droplets/containers. Your application logs are streamed in your dashboard and can be streamed using the Nanobox CLI.

Although every app starts out on a single droplet with containerized components, you can break components out into individual droplets and/or scalable clusters through the Nanobox dashboard. Nanobox handles the deep DevOps stuff so you don't have to. Enjoy!