LazerCube / django-quickstart-celery-docker

Quickstart for Django and Celery, using Ngnix, Gunicorn, PostgreSQL utilising Docker containers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Django + Ngnix + Celery + Docker

Build Status

Django 3.0.x Python 3.9

Prerequisites

  • Docker Machine - For running on mac and windows machines.
  • Docker - Runs either with Docker machine or on a linux machine.

Getting started

To run the app, docker and docker-compose must be installed on your system. For installation instructions refer to the Docker docs.

Docker machine (skip if not applicable)

  1. Start new machine - docker-machine create -d virtualbox dev;
  2. Configure your shell to use the new machine environment - eval $(docker-machine env dev)

Note: For Docker terminal on vscode, windows use eval $(docker-machine env dev --shell bash)

Add Enviroment File

Create a .env file in the root of the project for any enviroment variables for the project.

Example config:

# Add Environment Variables

COMPOSE_PROJECT_NAME=quickstart

SECRET_KEY=aaabbbccc
DB_NAME=postgres
DB_USER=postgres
DB_PASS=postgres
DB_SERVICE=postgres
DB_PORT=5432

#RABBITMQ_ERLANGCOOKIE=SWQOKODSQALRPCLNMEQG
RABBITMQ_DEFAULT_USER=rabbitmq
RABBITMQ_DEFAULT_PASS=rabbitmq
RABBITMQ_DEFAULT_VHOST=vhost1

DJANGO_LOG_LEVEL=DEBUG

Compose

The app can be run in development mode using Django's built in web server simply by executing

docker-compose up

To remove all containers in the cluster use

docker-compose down

To run the app in production mode, using gunicorn as a web server and nginx as a proxy, the corresponding commands are

docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
docker-compose -f docker-compose.yml -f docker-compose.prod.yml down

Swarm

It's also possible to use the same compose files to run the services using docker swarm. Docker swarm enables the creation of multi-container clusters running in a multi-host environment with inter-service communication across hosts via overlay networks.

docker swarm init --advertise-addr 127.0.0.1:2377
docker stack deploy -c docker-compose.yaml -c docker-compose.prod.yaml proj

It should be noted that the app will not be accessible via localhost in Chrome/Chromium. Instead use 127.0.0.1 in Chrome/Chromium.

To bring down the project or stack and remove the host from the swarm

docker stack rm proj
docker swarm leave --force

Getting internal ip

In order to view in a web browser you to grab the docker-machine ip.

docker-machine ip dev

Useful Commands

  • docker-compose logs -f <container> - Set terminal to view and follow logs for specified container.

Running the tests

Explain how to run the automated tests for this system

Deployment

Add additional notes about how to deploy this on a live system

Built With

  • Django - The web framework used
  • Python-pip - Dependency Management
  • Nginx - Open-source, high-performance HTTP server
  • Gunicorn - Python WSGI HTTP Server for UNIX
  • Docker - Securely build, share and run any application, anywhere.
  • Celery - Asynchronous task queue/job queue based on distributed message passing.
  • RabbitMQ - Open source message broker
  • Wait-For-it - Bash script to test and wait on the availability of a TCP host and port

About

Quickstart for Django and Celery, using Ngnix, Gunicorn, PostgreSQL utilising Docker containers.


Languages

Language:Python 69.1%Language:CSS 15.9%Language:HTML 7.4%Language:Shell 6.6%Language:Dockerfile 1.0%