Deploying a Django application using the Cookiecutter Django template on a DigitalOcean droplet is a powerful way to get your app running in production with Docker. This guide walks you through the full process — from droplet setup to live deployment — following modern best practices for security and performance.
1. Prerequisites
Before starting, ensure the following:
- You have a domain name ready with access to its DNS records
- A DigitalOcean Droplet running Ubuntu 22.04
- Basic knowledge of using the terminal
Reference: Initial Server Setup with Ubuntu 22.04
2. DNS Configuration
Set up your DNS A records to point to your droplet’s IP address. This is necessary for domain access later on.
3. Initial Server Setup
Connect to the Droplet
ssh root@<your-ip-address>
Update System Packages
apt-get update && apt-get upgrade
Add a New User
adduser <username>
usermod -aG sudo <username>
Configure Firewall
ufw allow OpenSSH
ufw allow https
ufw enable
Copy SSH Keys to New User
rsync --archive --chown=<username>:<username> ~/.ssh /home/<username>
Reconnect as the New User
ssh <username>@<your-ip-address>
4. Harden SSH Configuration
Edit the SSH config:
sudo nano /etc/ssh/sshd_config
- Change PermitRootLogin yes → no
- Change PasswordAuthentication yes → no
Save and exit (Ctrl + X, then Y, then Enter), then reboot:
sudo reboot
5. Install Docker and Docker Compose
Follow the official Docker docs for Ubuntu:
Add your user to the Docker group:
sudo usermod -aG docker $USER
6. Set Up GitHub SSH Key for CI/CD
Generate SSH key:
ssh-keygen -t ed25519 -C "your_email@example.com"
cat ~/.ssh/id_ed25519.pub
Add the public key to your GitHub account under Settings → SSH and GPG Keys.
7. Clone Project & Prepare Files
Clone your Cookiecutter Django project:
Transfer your .env files to the server. Use FileZilla with these settings:
- Protocol: SFTP
- Host: Your droplet IP
- Logon Type: Key File
- Key File: Path to your SSH private key
Tip: On macOS/Linux, press Cmd+Shift+. or Ctrl+H to show hidden files.
8. Launch Your Django App in Docker
Build and run containers:
docker compose -f docker-compose.production.yml up --build -d
Stream logs:
docker compose -f docker-compose.production.yml logs -f
Apply migrations:
docker compose -f docker-compose.production.yml run --rm django python manage.py migrate
Create a superuser:
docker compose -f docker-compose.production.yml run --rm django python manage.py createsuperuser
9. Set the Correct Domain in Django Sites Framework
Run a shell in your app:
docker compose -f docker-compose.production.yml run --rm django python manage.py shell
Update the site domain:
from django.contrib.sites.models import Site
site = Site.objects.first()
site.domain = 'yourdomain.com'
site.save()
10. Update Project Configuration for Production
Check and update the following:
- ALLOWED_HOSTS in .envs/.production/.django
- Domain references in compose/production/traefik.yml
Rebuild with changes:
docker compose -f docker-compose.production.yml up --build -d
11. Final Notes & Troubleshooting
- Login Issues: A 500 error on login may mean the email backend (like Mailgun or SendGrid) is misconfigured.
- Local Development Best Practice: Always generate and commit migrations locally. Never run makemigrations on the server.
Conclusion
Your Django application should now be live and accessible via your custom domain. With Docker and proper server configuration, your production environment is scalable, secure, and easy to maintain.
If you're planning to launch more apps or expand the setup, consider setting up CI/CD with GitHub Actions and managing secrets with environment-specific .env files and encrypted secrets.