rx-36

Deploying to the DigitalOcean App Platform - Using Docker Only for Local Development

This article explains how to deploy a web application using the DigitalOcean App Platform. For beginners in web development, one of the biggest hurdles to launching their own apps is deployment, and many struggle with this process even when following reliable tutorials.

I've been using DigitalOcean for 5–6 years. Initially, I used Droplets (VPS) for deployment. While Droplets are affordable, scalable, and flexible—making them ideal for individual developers—they aren't designed for simplified deployments. Configuring the web server and database from scratch can be quite tedious. I remember spending several days troubleshooting my first deployment with Droplets.

However, the release of DigitalOcean's App Platform in 2020 drastically simplified the deployment process. While the App Platform is slightly more expensive than Droplets, the ease of use more than justifies the cost.

This tutorial provides a stress-free and quick deployment method using the App Platform, ensuring that even those new to Django development can deploy their applications with ease. Although I use Docker in my development environment, deployment is even simpler without Docker, so we'll focus on that approach here.

Please note that this article contains affiliate links.

  1. Introducing the Repository I Created for this Deployment Tutorial

2. The Settings Directory Structure

Properly structuring your settings is essential when deploying Django applications to production. While a single settings.py file is common during development, some settings shouldn't be pushed to production, while others require environment-specific configurations.

Therefore, instead of a single settings.py, we create a settings directory, splitting settings.py into multiple files within this directory. Only the necessary files are then pushed to production.

The settings folder contains:

settings/__init__.py: This file tells Python to treat the settings directory as a package. It also contains the logic to determine which settings file to load based on the current environment.

settings/base.py: This file houses common settings shared across all environments, such as INSTALLED_APPS, MIDDLEWARE, and TEMPLATES.

settings/dev.py: This file contains development-specific settings, like DEBUG = True, logging configurations, and any development tools or libraries.

settings/prod.py: This file holds production-specific settings, including database credentials, security configurations (e.g., ALLOWED_HOSTS), and optimization settings.

3. Writing Each File for Production Deployment

4. Reviewing Other Necessary Files

5. Development-Only Files (Exclude from Deployment)

The following files are for development only and should be excluded from deployment:

docker-compose.yaml: The App Platform doesn't support docker-compose.yaml, so exclude this file.

Dockerfile: While the App Platform does support deployments using a Dockerfile, doing so requires merging the configurations from your docker-compose.yaml into the Dockerfile. This adds extra configuration steps and complexity. For simplicity, I recommend avoiding Dockerfile deployments on the App Platform.

.env: Manually enter the .env variables into the App Platform's environment variable settings. The .env file itself isn't used directly.

old_settings.py: My repository includes old_settings.py, which is the original, unsplit settings.py file. This file isn't needed in the current configuration and should be excluded from deployment.

Configuring the .gitignore File

Ensure these four files are in your .gitignore:

  • docker-compose.yaml
  • Dockerfile
  • .env
  • old_settings.py

Including these files may cause deployment issues. See my .gitignore file for reference.

6. Pushing to Your GitHub Repository

Next, push your code to GitHub.

Note: I've set my deployment target branch to develop, not the default main branch. This distinction is important for the next step, so please keep it in mind.

7. Deploy Using the DigitalOcean App Platform

Once you've prepared your project for deployment, create a DigitalOcean account and deploy it to the App Platform.

While the standard App Platform deployment process is documented elsewhere, this guide explains the steps tailored to our setup.

Note: If you don't have a DigitalOcean account yet, sign up at DigitalOcean App Platform. They're currently offering $200 in free credits—plenty to get you started.

Deploying your application involves four main steps:

  1. Creating a DigitalOcean account and connecting your GitHub account
  2. Configuring the database
  3. Entering environment variables
  4. Deploying the application

Let's walk through these steps with screenshots.

7-1. Creating a DigitalOcean Account and Connecting Your GitHub Account

Creating a DigitalOcean account is straightforward, so I'll skip those details. Here's what to do after logging in:

7-2. Configuring the Database

7-3. Entering Environment Variables

7-4. Deploying the Application

Conclusion

Deploying to the App Platform is quite straightforward, but there are a few key points to remember. Based on my experience with the App Platform, here are some tips for smooth deployments:

1. Include Gunicorn in requirements.txt: While Django's development server is sufficient during development, you need a WSGI server like Gunicorn in production to interface between the web server and your Python application. Without it, deployment will fail.

2. Create a Procfile: Without a Procfile, the App Platform might not know how to start your application, leading to errors. Creating a Procfile prevents this.

PaaS (Platform as a Service) has significantly simplified application deployments. I believe DigitalOcean, in particular, offers one of the easiest and most affordable deployment solutions. Take advantage of these services to launch your own projects with confidence!