Deploy to theDigitalOcean App Platform. Docker uses only local environment

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. Explaining the settings Directory within the Project

A crucial aspect of deploying Django applications to production is properly structuring your settings. While a single settings.py file is common during development, some settings within this file 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 the contents of settings.py into several files within this directory. Only the necessary files are then pushed to production.

Here's the structure of the settings folder:

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. How to Write Each File for Production Deployment

4. Reviewing Other Necessary Files

5. Files Needed Only for Development (Exclude from App Platform Deployment)

The following files are required only for development and should not be included in the App Platform deployment process:

docker-compose.yaml: The App Platform doesn't directly support docker-compose.yaml. Therefore, you don't need to include this file when deploying.

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 to the deployment process. For simplicity, I recommend avoiding Dockerfile deployments on the App Platform.

.env: You'll need to manually enter the contents of your .env file 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 is not needed in the current configuration and should be excluded from deployment.

Configuring the .gitignore File

Make sure the four files mentioned above (docker-compose.yaml, Dockerfile, .env, and old_settings.py) are listed in your .gitignore file. Including these files can negatively impact your deployment. Carefully configuring your .gitignore is crucial to prevent unnecessary files from being accidentally included in the App Platform deployment process.

I've created a .gitignore file that you can use as a reference.

6. Pushing to Your GitHub Repository

After configuring these files, push them to your GitHub repository. I'll omit the details of pushing to GitHub, as numerous tutorials are readily available online.

7. Setting up 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, including other blogs and the official documentation, I'll explain the steps tailored to our current setup.

You can access the DigitalOcean App Platform here. And, good news! They're currently offering $200 in free credits.

Deploying your application involves four main steps:

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

Let's break down these steps with screenshots.

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

Creating a DigitalOcean account is straightforward, so I'll omit those details. Here's what to do after you've created your account and navigated to the main dashboard:

7-2. Configuring the Database

7-3. Entering Environment Variables

7-4. Deploying the Application

Deploying to the App Platform is quite easy, 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 where 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. Leverage these services to launch your own projects with confidence!