📦

Setting Up and Deploying a Django Application

Jul 23, 2024

Setting Up and Deploying a Django App Using Docker Compose on AWS

Overview

  • This guide covers setting up a Django application and deploying it to an AWS server using Docker Compose for a lightweight deployment.
  • Emphasis on ease of deployment with low overhead but note scalability limitations for high traffic applications.

Necessary Prerequisites

  • Software & Accounts Needed:
    • Docker installed
    • Code editor (Visual Studio Code suggested)
    • GitHub account
    • Access to AWS (using a server eligible for the free tier)
  • Familiarity with SSH authentication is essential.

Creating the GitHub Repository

  1. Log in to GitHub and create a new repository for your app (e.g., django-docker-compose-deployment).
  2. Set it to public/private as needed and initialize it with a .gitignore using Python settings and a README.
  3. Clone the repository to your local machine.

Setting Up the Django Project

  1. Create a requirements.txt file in the project root with dependencies; start with:
    • django>=3.2.3,<3.3
  2. Create a Dockerfile in the project root:
    • Use the base image python:3.9-alpine3.13 for a lightweight setup.
    • Set environment variables and copy requirements.txt and app source files appropriately.
  3. Set the work directory to /app and expose port 8000.

Docker Compose Setup

  1. Create a docker-compose.yml file:
    • Define version 3.9 of the Docker Compose syntax.
    • Add a service for the app that maps ports properly and handles volumes for source updates.
  2. Create a .dockerignore file to exclude unnecessary files from the Docker build context.
  3. Build the Docker container using: docker-compose build

Configuring Django Application

  1. Start Django project with:

docker-compose run --rm app sh -c "django-admin startproject app ."

2. Modify `settings.py` to handle secret keys using environment variables. - Set `DEBUG` environment variable. - Manage `ALLOWED_HOSTS` with external hostnames. 3. Add PostgreSQL configuration to use as your database. 4. Ensure migrations occur correctly. ### Setting Up NGINX Reverse Proxy 1. Create a directory called `proxy` for NGINX setup. 2. Create necessary NGINX configuration files: `uwsgi_params` and `default.conf.tpl`. This introduces static/media file handling for efficient deployment. 3. Establish `run.sh` script to set up NGINX and start the server. ## Finalizing Docker Configuration - Modify existing Dockerfiles as necessary to copy in scripts/configurations. - Create a separate deployment `docker-compose-deploy.yml` with changes for a production environment. ### Setting Up AWS EC2 Instance 1. Create an EC2 instance in the AWS management console with default configurations (t2.micro for free tier). 2. Set up security groups to allow HTTP and SSH traffic. 3. Install Git and Docker on the EC2 instance after connecting via SSH. 4. Clone your GitHub repository onto the server, including setting up deploy keys for private repos if needed. ### Deploying the Application 1. Upload code changes and run `docker-compose -f docker-compose-deploy.yml up -d` to launch services in a detached mode. 2. Attach ports properly for external access and static file handling. 3. Test the application in your browser via the EC2 public DNS. ## Conclusion - This method allows for rapid deployment but requires best practices in managing sensitive information via environment variables. - For ongoing updates, use git commands to pull changes, followed by building/updating containers as necessary. ## Notes - If you encounter issues related to port conflicts, check existing applications using the port on your local machine or AWS instance. - This process leverages Docker for consistent deployments across environments and facilitates easy scaling in production via better practices.