Production Deployment with Docker Compose

Production Deployment with Docker Compose

 

Deploying a web application in production requires a setup that is scalable, secure, and easy to manage. Docker Compose simplifies this process by allowing you to define and run multiple services—such as Django, PostgreSQL, and Nginx—in a single configuration.

In this guide, we’ll deploy the Django web application that we created in the previous section, transitioning it from local development to a production-ready environment using AWS Lightsail, Docker, and Nginx. By containerizing the application with Docker, we ensure that the same code runs consistently across different environments, demonstrating the portability and reliability of our setup.

If you prefer to jump straight into setting up the production server, you can skip the first step and clone the preconfigured code from GitHub.

Step 1: Initiate Git and Push the Code to GitHub

If you haven’t initiated a Git repository for your project, follow these steps to set it up and push your code to GitHub.

1. Initialize Git Repository (If Not Already Done)

Navigate to the project directory and initialize a Git repository:

cd path/to/your/project
git init

2. Add and Commit the Code

After updating the settings.py and docker-compose-prod.yml for production, commit the changes:

git add .
git commit -m "Updated settings for production deployment"

If this is your first commit, a simple message like 'First commit' is sufficient.

3. Connect to a GitHub Repository

If you haven’t already connected your project to a GitHub repository, create one on GitHub and link it to your local repository:

git remote add origin https://github.com/your-username/your-repository.git

4. Push Code to the GitHub Main Branch

Since we are handling different settings using the .env file, we can use the main branch instead of a separate production branch. Push the code to GitHub:

git branch -M main
git push -u origin main

To learn how to set up and use Git and GitHub, check out our Git and GitHub Introduction Guide. It provides a visual, step-by-step walkthrough.

Step 2: Prepare AWS Lightsail Instance

To deploy the application, you first need to set up a virtual server on AWS Lightsail. This instance will serve as the production environment where your Django application, database, and Nginx web server will run. We are using AWS Lightsail because it provides a s

FAQ: Production Deployment with Docker Compose

What is the purpose of using Docker Compose in production deployment?

Docker Compose allows you to define and run multiple services, such as Django, PostgreSQL, and Nginx, in a single configuration, simplifying the deployment process and ensuring consistency across different environments.

Why is AWS Lightsail chosen for deploying the application?

AWS Lightsail is chosen because it provides a simplified and cost-effective way to deploy applications compared to AWS EC2, with pre-configured virtual servers that make it easier and quicker to set up a production environment.

How do you ensure the security of sensitive information in a production setup?

In a production setup, sensitive information like SECRET_KEY and database credentials should be stored in environment variables or secret management tools, and not in public repositories, to prevent unauthorized access and data breaches.

What role does Nginx play in the deployment process?

Nginx acts as a reverse proxy for the Django application, forwarding client requests to the Gunicorn server running inside the Docker container and serving static files efficiently.

What should be done after testing the deployment to avoid unnecessary costs?

After testing, you can stop the AWS Lightsail instance to preserve data and prevent charges, or delete it if no longer needed, ensuring to back up any important data before deletion.