Skip to main content

Deploy the Django app to Ubuntu server as docker container.

Project description

Dockdj

Django app deployment made easy

Dockdj is a command line tool that allows you to deploy any Django app to single Ubuntu server.

This tool is inspired by Meteor up tool which is used to deploy Meteor.js app.

You can install and use Dockdj on Linux, Mac and Windows. It can deploy to servers running latest Ubuntu.

Dockdj is powered by Docker compose, making deployment easy to manage.

Requirement

This tool needs Python > 3.6

Install

Install the dockdj using following command

$ pip install dockdj

Usage

Lets say you have a Django project with channels (asgi) and celery worker. And celery worker needs rabbit mq. Dockdj can help to deploy this app easily with following architecture. Each box inside server is docker container.

+-----------------------------------------------------------------------------+
|                      Ubuntu server 18.04 LTS                                |
|                                                                             |
|                                                   +----------------+        |
|                                          +--------+   wsgi:80      |        |
|        +---------------+                 |        |  (gunicorn)    |        |
|        |  nginx:80     +-----------------+        +----------------+        |
|        |  static files +-----------------+                                  |
|        +---------------+                 |                                  |
|           localhost:8000                 |        +----------------+        |
|                                          |        |   asgi:80      |        |
|                                          +--------+  (daphne)      |        |
|                                                   +----------------+        |
|                                                                             |
|                                                                             |
|                                                                             |
|                                                                             |
|                                                                             |
|                                                                             |
|         +--------------+                          +----------------+        |
|         |  celery      +--------------------------+   rabbit mq    |        |
|         |              |                          |                |        |
|         +--------------+                          +----------------+        |
|                                                                             |
|                                                                             |
|             Example deployment of django app using dockdj                   |
+-----------------------------------------------------------------------------+
How to deploy as show in diagram?

Create a sibling to the django project directory for saving dockdj settings files which will be used to deploy the django app

$ mkdir deploy

parent
    - hello_world
    |    -hello_world
    |        -wsgi.py
    - deploy

$ cd deploy

$ dockdj init

This will create two files dockdj.yaml and settings.py

parent
    - hello_world
    |    -hello_world
    |        -wsgi.py
    - deploy
        - dockdj.yaml
        - settings.py

Modify the dockdj.yaml file to enter server configuration and app details. Initial file looks as follows. Remove the optional/unused configuration according to your needs.

---
server:
  host: 1.2.3.4
  # must be sudo without password
  username: root
  pem: "/home/user/.ssh/id_rsa"
app:
  # Project name used by script to name docker
  name: hello_world_stage
  settings: 'hello_world/settings.py'
  wsgi:
    app: 'hello_world.wsgi:application'
  asgi: # optional if app uses agsi like channels
    app: 'hello_world.asgi:application'
    paths: # used by nginx to route to asig
      - /ws
      - /some_thing_else
  celery: # optional for celery django app
    app: 'hello_world'
  requirements_file: 'requirements.txt'
  extra_files: # Optional: config, certs any other files loaded to /app/extra_files/* in docker container
    - /path/to/abc.txt
  # Django project directory
  path: "/path/to/django/code/hello_world"
  docker:
    image: python:3.7

compose:
  services:
    celery:
      depends_on:
        - rabbitmq
    rabbitmq:
      image: rabbitmq

settings.py fill contains settings which will be appended to your django app's settings.py file. You can enter production related settings or override development settings here. Initial settings.py file looks like as follows.

DEBUG = False

# Add the your servers host name or ip address
ALLOWED_HOSTS = ['1.2.3.4', 'www.example.com']

CELERY_BROKER_URL = 'amqp://guest:guest@rabbitmq:5672//'

After editing these two files appropriately run following command to setup server. This will install docker in Ubuntu servers if not already installed.

$ dockdj setup

To deploy the django app run following.

$ dockdj deploy

This will create docker images of your Django app. Collects the static file and adds it to nginx container. Runs all required containers as specified in dockdj.yaml. Binds the port 80 of nginx container to port 8000 of Ubuntu host. Port binding can be modified by adding following configuration in `dockdj.yaml'

compose:
  services:
    nginx:
      ports:
      - 8004:80

dockdj stop

Run following command to stop all containers

$ dockdj stop

dockdj restart

Run following command to restart all containers

$ dockdj restart

dockdj logs

Run following command to see logs of all containers

$ dockdj logs

dockdj one-off

You can run one-off script on server using one-off sub command as follows.

$ dockdj one-off python manage.py migrate

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dockdj-2.2.2.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

dockdj-2.2.2-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file dockdj-2.2.2.tar.gz.

File metadata

  • Download URL: dockdj-2.2.2.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.7.1

File hashes

Hashes for dockdj-2.2.2.tar.gz
Algorithm Hash digest
SHA256 338c47c30956344a0c1ebfaba0dd231b33746e280813c6803767f5f508cdcbaa
MD5 fef92190f2e5dc4213ff91051254a77d
BLAKE2b-256 1bece633807dad5111165d7408641e172c03f32d3840eff194bfb8e7c322c8d9

See more details on using hashes here.

File details

Details for the file dockdj-2.2.2-py3-none-any.whl.

File metadata

  • Download URL: dockdj-2.2.2-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.7.1

File hashes

Hashes for dockdj-2.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5541ea2b39fdff8bd6e3861dba0be77b8cf72392f4db858008b9a1bfece183e3
MD5 27806f6bb9287fe4e673df8becfbd18b
BLAKE2b-256 f9d871d7977967bae5b9c1926846f98fa28144917cf7b2282bfb7ac7a91731d2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page