Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Docker BasicsChapter 6. Docker Compose and Deployment Practice

Production Deployment with Docker Compose

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

More Topics to Explore

How to Make a Pull Request on GitHub

Request for Review and Merge – Pull Request

Adding Files to the Staging Area with git add

Add Files to Staging Area – git add

Launching Your Git Project: Key Steps Explained

Project Initiator – Key Steps To Launch Git Project

Modifying Access Modes with chmod Command

chmod (Change Access Mode)

Exploring GitHub's Additional Features

GitHub Other Features

How to Make a Pull Request on GitHub

Request for Review and Merge – Pull Request

Adding Files to the Staging Area with git add

Add Files to Staging Area – git add

Launching Your Git Project: Key Steps Explained

Project Initiator – Key Steps To Launch Git Project

Modifying Access Modes with chmod Command

chmod (Change Access Mode)

Exploring GitHub's Additional Features

GitHub Other Features

Subscribe now for
uninterrupted access.

Sign up for free trial

Docker Basics
Course Content

Chapter 1. Docker Introduction

Computing Environment and Dependency Conflict

Containers vs. Virtual Machines

What Is Docker?

Chapter 2. Getting Started with Docker

Setting Up Docker Environment

Overview of Docker Workflow

Docker Commands

Chapter 3. Docker Image and Container

Docker Images and Registries (Docker Hub)

Docker Container Lifecycle

Advanced Container Lifecycle Management

Docker Commands to Interact with Inside of Containers

Chapter 4. Docker Networking and Storage

Docker Networking

Persistent Storage with Docker Volumes

Chapter 5. Building and Sharing Docker Images

What Is a Dockerfile?

Build Context and .dockerignore File

Dockerfile Syntax

Sharing Your Docker Images

Chapter 6. Docker Compose and Deployment Practice

Writing a Docker Compose File

Docker Compose Commands

Web App Development with Docker Compose

Production Deployment with Docker Compose

Advancing Your Docker Skills

Chapter 1. Docker Introduction

Computing Environment and Dependency Conflict

Containers vs. Virtual Machines

What Is Docker?

Chapter 2. Getting Started with Docker

Setting Up Docker Environment

Overview of Docker Workflow

Docker Commands

Chapter 3. Docker Image and Container

Docker Images and Registries (Docker Hub)

Docker Container Lifecycle

Advanced Container Lifecycle Management

Docker Commands to Interact with Inside of Containers

Chapter 4. Docker Networking and Storage

Docker Networking

Persistent Storage with Docker Volumes

Chapter 5. Building and Sharing Docker Images

What Is a Dockerfile?

Build Context and .dockerignore File

Dockerfile Syntax

Sharing Your Docker Images

Chapter 6. Docker Compose and Deployment Practice

Writing a Docker Compose File

Docker Compose Commands

Web App Development with Docker Compose

Production Deployment with Docker Compose

Advancing Your Docker Skills

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.