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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_discovery-0.0.6.tar.gz.
File metadata
- Download URL: django_discovery-0.0.6.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
330027880bad106d51ed102908f5b62c64ba9b6972217536ba46158e093428b9
|
|
| MD5 |
0da91284d64599d4bb81ce572ffc91fd
|
|
| BLAKE2b-256 |
d107b9ac1aa7819c52b27b070a85504c7e8ad81a2945dee84dd5b7c90b6a7734
|
File details
Details for the file django_discovery-0.0.6-py3-none-any.whl.
File metadata
- Download URL: django_discovery-0.0.6-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5664f47dc7796677b4fa80eb35e9ed20ce7f078e7b12683ede6b8e0f73243b07
|
|
| MD5 |
dbc3718c82807cd0dd54fe84116c55f6
|
|
| BLAKE2b-256 |
996ed7d6630374fe43796344549198a7412e60feba4f0b8d1855da98cc189751
|