Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Fabricio: Django

This example shows how to deploy Django configuration consisting of a single container based on custom image which automatically built from provided Dockerfile.

Requirements

  • Fabricio 0.4.6 or greater
  • Vagrant
  • One from the list of Vagrant supported providers (this example was tested with VirtualBox)
  • Docker for Linux/Mac/Windows
  • Docker registry which runs locally on 5000 port, this can be reached out by executing following docker command: docker run --name registry --publish 5000:5000 --detach registry:2

Files

  • project, Django project
  • Dockerfile, used for building image
  • fabfile.py, Fabricio configuration
  • README.md, this file
  • Vagrantfile, Vagrant config

Virtual Machine creation

Run vagrant up and wait until VM will be created.

List of available commands

fab --list

Deploy

fab django

Rollback

Copy 0003_copy_this_to_parent_folder.py migration file from project/app/migrations/new/ directory to the parent one (project/app/migrations/) and then make deploy again:

fab django

This will apply new migration. After that run 'rollback' command which should remove newly applied migration:

fab django.rollback

Customization

See also "Hello World" Customization section.

DJANGO_SETTINGS_MODULE customization

You can provide custom settings to each infrastructure you have by providing callable DJANGO_SETTINGS_MODULE env option:

from fabric import api as fab
from fabricio import tasks
from fabricio.apps.python.django import DjangoContainer

def django_settings(**additional_variables):
    def _settings(
        container,  # type DjangoContainer
    ):
        # select settings module depending on infrastructure name
        # (see 'Infrastructures and roles' example)
        settings = ('settings', fab.env.infrastructure)
        env = ['DJANGO_SETTINGS_MODULE=%s' % '.'.join(settings)]
        env.extend(map('='.join, additional_variables.items()))
        return env
    return _settings

django = tasks.ImageBuildDockerTasks(
    service=DjangoContainer(
        name='django',
        image='django',
        options={
            'publish': '8000:8000',
            'stop-signal': 'INT',
            'volume': '/data/django:/data',
            'env': django_settings(
                FOO='foo',
            ),
        },
    ),
)

DjangoService

To use Django as Docker service (such as described in Docker services example) one can use DjangoService instance:

from fabricio import tasks
from fabricio.apps.python.django import DjangoService

django = tasks.ImageBuildDockerTasks(
    service=DjangoService(
        name='django',
        image='django',
        options={
            'stop-signal': 'INT',
            'env': 'DJANGO_SETTINGS_MODULE=settings',
        },
    ),
)

DjangoStack

Also, Django can be a part of Docker stack (see Docker stacks example):

from fabricio import tasks
from fabricio.apps.python.django import DjangoStack

django = tasks.ImageBuildDockerTasks(
    service=DjangoStack(
        # stack name
        name='django-stack',
        
        # image tag which will be built and used to apply migrations on
        # (all other stack services images must be ready to the moment of stack deploy)
        image='django-app',
        
        # safe options are options passing to container
        # that does 'migrate' (or 'migrate-back') operation
        safe_options={
            'stop-signal': 'INT',
            'env': 'DJANGO_SETTINGS_MODULE=settings',
        },
    ),
)

DjangoKubernetes

To use Django within the Kubernetes cluster one can take DjangoKubernetes service type:

from fabricio import tasks
from fabricio.apps.python.django import DjangoKubernetes

django = tasks.ImageBuildDockerTasks(
    service=DjangoKubernetes(
        # configuration name
        name='django-k8s',

        # image tag which will be built and used to apply migrations on
        # (all other configuration images must be ready to the moment of configuration deploy)
        image='django-app',

        # safe options are options passing to container
        # that does 'migrate' (or 'migrate-back') operation
        safe_options={
            'stop-signal': 'INT',
            'env': 'DJANGO_SETTINGS_MODULE=settings',
        },
    ),
)

See also Kubernetes example.

New in 0.5

Issues

  • Windows users may fall into trouble with VirtualBox and Hyper-V, the latter is used by "native" Docker for Windows. Try to disable Hyper-V and use Docker Toolbox instead in such case