Deploy Django in Coolify
1. Update Dockerfile
Section titled “1. Update Dockerfile”## NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"## PLEASE DO NOT EDIT IT DIRECTLY.#
# Use Python 3.12 slim imageFROM python:3.12-slim
# Set the working directory in the containerWORKDIR /app
# Copy the current directory contents into the containerCOPY . /app/
# Install system dependenciesRUN apt-get update && apt-get install -y \ libpq-dev gcc \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* # add any other system dependencies here
# Install Python dependenciesRUN pip install --upgrade pipRUN pip install -r requirements.txt
# Collect static files and migrate databaseRUN python manage.py collectstatic --noinputRUN python manage.py makemigrationsRUN python manage.py migrate# RUN python manage.py apps_loaddata # enable this if you have data (fixtures)
# Expose the port that Django/Gunicorn will run onEXPOSE 80
# Command to run Gunicorn with the WSGI application for productionCMD ["gunicorn", "--bind", "0.0.0.0:80", "project.wsgi:application"]2. Setup AWS S3
Section titled “2. Setup AWS S3”TODO
3. Create coolify project
Section titled “3. Create coolify project”- Go to https://apps.darideveloper.com/projects
- Create new project (if required) with project name, example: “project-name”
- Open the project
- Open “production” environment
4. Create database instance
Section titled “4. Create database instance”4.1 Validate database free ports
Section titled “4.1 Validate database free ports”Tun this command to detect the next free port to use.
sudo ss -tuln4.2. Create instance
Section titled “4.2. Create instance”- Click in ”+ Add New Resource”
- Select “Postgresql”
- Select “PostgreSQL 16 (default)”
- Change name to “db”
- Change username and password
- Set next db free port in “Public Port”
- Start / Deploy database
- Enable checkbox “Make it publicly available”
- Check database connection with PGAdmin
- Create new database inside the database instance, with project name (with PGAdmin)
5. Update db connection in .env.prod
Section titled “5. Update db connection in .env.prod”- Open .env.prod file (create it if it doesn’t exist)
- Change env in .env file to “prod”
ENV=prod- Update database credentials
DB_HOST=server ipDB_PORT=db free portDB_NAME=db nameDB_USER=db usernameDB_PASSWORD=db password5.1. Run migrations and create superuser to check connection
Section titled “5.1. Run migrations and create superuser to check connection”- Run migrations and create superuser
python manage.py makemigrationspython manage.py migratepython manage.py createsuperuser- Run project in local to check admin login
- Reset back to dev env in .env file
ENV=dev6. Enable database backups
Section titled “6. Enable database backups”- Open database instance in coolify
- Click in “Backups Tab”
- Click in ”+ Add”
- Set a cronjob ro every day at random time, example:
7 15 * * * # every day at 15:07- Click in “Save”
- Open Backup created
- Enable checkbox “Backup All Databases”
- Check if working clicking in “Backup Now”
- Check backup created in “Status: success”
7. Create django app
Section titled “7. Create django app”- Go to coolify project
- Open “production” environment
- Click in ”+ Add New Resource”
- Select “Public Repository”
- Paste github repository url
- Click in “Check Repository”
- In Build Pack select “Dockerfile”
7.1 General
Section titled “7.1 General”- Go to “General” tab
- Change name to repository name, example: “project-name”
- Update subdomain to repository name, example: “https://project-name.apps.darideveloper.com”
- Set “Ports Exposes” to “80”
- Press “Enter” at last field to save changes
7.2. Advanced
Section titled “7.2. Advanced”- Go to “Advanced” tab
- Set “Custom Container Name” to project name, example: “project-name”
- Press “Enter” at last field to save changes
7.3. Environment Variables
Section titled “7.3. Environment Variables”- Open project database instance in coolify, in new tab
- Copy Postgres URL (internal)
- Go back to django app in coolify
- Go to “Environment Variables” tab
- Click in “Developer view”
- Setup all env variables from .env.prod file (copy and paste)
- Update database host and port, from Postgres URL (internal)
- Update host to current project domain
- Add project domain to ALLOWED_HOSTS (without https)
- Add project domain to CSRF_TRUSTED_ORIGINS
- Add project domain to CORS_ALLOWED_ORIGINS
- Click in “Save All Environment Variables”
7.3.1. Setup build variables
Section titled “7.3.1. Setup build variables”- Go back to regular view clicking in “Environment Variables” tab
- Mark all variables as “Build Variable?” (checkbox)
8. Deploy project
Section titled “8. Deploy project”- Deploy project clicking in “Deploy” button
- Check deploy status in “Status: success”
- Check project working in the domain (wait for the domain ssl to be available)
9. Enable auto deploy
Section titled “9. Enable auto deploy”TODO