Essential Guidelines for Publishing Your Django Project Online

Essential Guidelines for Publishing Your Django Project Online

Django-based "My Notes" Application Deployment

Application Overview: "My Notes" is a simple note-taking application built using Django. It allows users to create, store, and manage notes, similar to notepad applications used on mobile devices.

Prerequisites:

  1. Docker & Docker Compose
    Ensure Docker and Docker Compose are installed on your machine. These tools will help in containerizing and orchestrating the application and its dependencies.

  2. Django and Gunicorn
    Django framework is required to run the web application. Gunicorn is used as the WSGI server for running the Django application in production.

  3. MySQL Database
    The application uses MySQL as the database to store notes and user data.

Deployment Steps:

  1. Docker Compose Setup
    The docker-compose.yml file is used to define the services for the application, including:

    • nginx: The reverse proxy server that handles HTTP requests.

    • django_app: The Django application server.

    • db: The MySQL database container.

  2. Dockerfile Configuration

    • The Dockerfile is responsible for setting up the environment for both Django and Nginx. Ensure it contains the necessary steps like copying project files, installing dependencies, and configuring the environment.
  3. Application Build
    The following services are configured in docker-compose.yml:

    • nginx: Acts as a reverse proxy for the application.

    • django_app: Hosts the Django application, runs migrations, and starts the Gunicorn server.

    • db: The MySQL container holds the data for the application.

  4. Database Setup
    Ensure the MySQL database is initialized with necessary credentials and configurations:

    • MYSQL_ROOT_PASSWORD is set.

    • MYSQL_DATABASE is defined for the application's data.

  5. Ports Configuration

    • Port 80 for Nginx (HTTP) and port 8000 for Django application.
      Ensure no conflicts with other running services on the same ports.
  6. Volumes Configuration
    Define volumes to persist data across restarts, especially for MySQL.

    volumes: - ./data/mysql/db:/var/lib/mysql

  7. Health Checks
    Define health checks for services to ensure they are running correctly:

    • Django App: Test access to the admin panel via HTTP.

    • MySQL: Ping the MySQL server to check its availability.

Running the Application:

Once all configurations are done, the application can be built and deployed using Docker Compose, which will:

  1. Build the images for all services.

  2. Start the containers for Nginx, Django, and MySQL.

Troubleshooting Docker Compose and Deployment Issues

1. Address Already in Use Error

  • Issue: The port is already in use by another process.

  • Solution:

    • Identify the process using the port (80 or 8000) using lsof or equivalent.

    • Terminate the conflicting process or choose a different port for the service in the docker-compose.yml file.

2. Container Name Conflict

  • Issue: A container with the same name already exists.

  • Solution:

    • Remove the existing container using the container ID or name.

    • Alternatively, rename the container in the docker-compose.yml.

3. No Permission to Read from Files

  • Issue: Insufficient permissions to access or read certain files (e.g., #ib_16384_0.dblwr).

  • Solution:

    • Ensure the appropriate permissions for the directory and files.

    • Use chmod to modify file permissions or adjust the file ownership if needed.

4. Memory or Disk Space Issues

  • Issue: Insufficient disk space or memory (e.g., error with mysql.ibd).

  • Solution:

    • Free up disk space by removing unnecessary files or containers.

    • Ensure that the Docker containers have enough disk space and memory allocated.

5. YAML Formatting Errors in Docker Compose

  • Issue: Incorrect YAML syntax (e.g., incorrect use of spaces, missing colons).

  • Solution:

    • Check for indentation errors and ensure that each key is properly mapped with values.

    • Use YAML validators to check the correctness of the file.

6. Cannot Find or Read Dockerfile

  • Issue: Dockerfile is missing or misplaced.

  • Solution:

    • Ensure the Dockerfile exists in the correct directory specified in the docker-compose.yml file.

    • If missing, create a valid Dockerfile or update the path in docker-compose.yml accordingly.

7. Build Context Issues (e.g., permissions or invalid files)

  • Issue: The build context or files required for the build are inaccessible.

  • Solution:

    • Verify the file paths in the docker-compose.yml.

    • Ensure all necessary files (e.g., requirements.txt, .env) are present and readable.

8. Docker Networking Issues (Binding Ports)

  • Issue: Docker can't bind to the specified port (80, 8000, etc.).

  • Solution:

    • Identify other services or processes occupying the port.

    • Change the port mapping in docker-compose.yml or stop the service using the port.

9. Service Dependency Issues

  • Issue: One service cannot start due to dependency issues (e.g., database not available).

  • Solution:

    • Ensure that services are correctly ordered in docker-compose.yml using the depends_on key.

    • Check the health check conditions to ensure services are started in the correct sequence.

10. File Permission or Volume Mount Issues

  • Issue: Cannot read from or write to mounted volumes (e.g., MySQL database files).

  • Solution:

    • Ensure the volumes specified in the docker-compose.yml file have correct permissions.

    • Adjust the file ownership or ensure Docker has sufficient access to the directories.

Conclusion:

This setup provides a straightforward way to deploy a Django-based "My Notes" application in a containerized environment using Docker and Docker Compose. Ensure that all configurations are correct and monitor the containers for any issues during deployment.

Checkout my GitHub repository, if you like to watch the whole project. https://github.com/Sudoharry/Live-Project-Devops/tree/main/Day02