Skip to main content

Docker service discovery for Django

Project description

This project provides a simple workflow for registering services in Django. It is highly opinionated and makes a lot of assumptions.

Goal

When developing a Django application, you often need external services such as a database, a key-value store, a message broker, etc. etc. I prefer to use Docker and Docker-Compose to have those services running when I develop my application.

django-discovery makes it easy to connect to those services, as long as you follow a specific workflow:

  • Development is done on your own machine, with the services running under Docker;

  • Testing is done by running both the services and the application using Docker Compose;

  • The production environment provides an SRV-lookup capable DNS. Examples are:

    • Running everything under Kubernetes

    • Running everything on Docker, configured to use an SRV-capable DNS such as Consul

Requirements

django-discovery requires Python 3 and Django 1.8 because we live in modern times.

Quickstart

The following example assumes a Django application that requires a MySQL database. Add the following to your settings.py:

from discovery import services

db = services.register('my_db', 'mysql', secrets=['mysql_user', 'mysql_database', 'mysql_password'])

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': db.secrets['mysql_database'],
        'USER': db.secrets['mysql_user'],
        'PASSWORD': db.secrets['mysql_password'],
        'HOST': db.host,
        'PORT': db.port,
    }
}

DEBUG = services.debug_mode

You can now run your application on localhost with your database under docker using the following docker-compose.yml:

my_db:
    image: mysql:5.6
    ports:
        - ":3306"
    environment:
        MYSQL_ROOT_PASSWORD: supersecret
        MYSQL_DATABASE: database_name
        MYSQL_USER: database_user
        MYSQL_PASSWORD: alsosecret

You can also run your application in its entirety in docker, using for example the following docker-compose file:

database:
    image: mysql:5.6
    environment:
        MYSQL_ROOT_PASSWORD: supersecret
        MYSQL_DATABASE: database_name
        MYSQL_USER: database_user
        MYSQL_PASSWORD: alsosecret

application:
    build: .
    links:
        - database:my_db
    ports:
        - "80:8080"
    env:
        DISCOVERY_MODE: env

And still using the same settings.py, you can even run your application under Kubernetes using the following definitions:

apiVersion: v1
kind: Service
metadata:
  name: my_db
spec:
  ports:
    - protocol: tcp
      port: 3306
apiVersion: v1
kind: Endpoints
metadata:
  name: my_db
subsets:
  - addresses:
    - ip: 1.2.3.4
  - ports:
    - port: 3306
apiVersion: v1
kind: Pod
metadata:
  name: my_application
  namespace: my_namespace
spec:
  containers:
     - name: my_application
       image: my/docker-image
       volumeMounts:
         - name: db_secrets
           mountPath: /etc/secrets/my_db
           readOnly: true
  volumes:
    - name: db_secrets
      secret:
        mysql_database: some_name
        mysql_user: some_user
        mysql_password: secret_password

Full Documentation

Working on it…

License

This project is licensed under the MIT license.

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

django_discovery-0.0.6.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

django_discovery-0.0.6-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file django_discovery-0.0.6.tar.gz.

File metadata

File hashes

Hashes for django_discovery-0.0.6.tar.gz
Algorithm Hash digest
SHA256 330027880bad106d51ed102908f5b62c64ba9b6972217536ba46158e093428b9
MD5 0da91284d64599d4bb81ce572ffc91fd
BLAKE2b-256 d107b9ac1aa7819c52b27b070a85504c7e8ad81a2945dee84dd5b7c90b6a7734

See more details on using hashes here.

File details

Details for the file django_discovery-0.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for django_discovery-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5664f47dc7796677b4fa80eb35e9ed20ce7f078e7b12683ede6b8e0f73243b07
MD5 dbc3718c82807cd0dd54fe84116c55f6
BLAKE2b-256 996ed7d6630374fe43796344549198a7412e60feba4f0b8d1855da98cc189751

See more details on using hashes here.

Supported by

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