这是indexloc提供的服务,不要输入任何密码
Skip to content

alexshin/event-app-docker-daphne

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker image for daphne (interface server for Django channels)

See https://github.com/django/daphne/ and https://channels.readthedocs.io/en/stable/.

This image is built over https://github.com/tbeadle/docker-daphne repository. Thank you for boilerplate

Building the image

docker-compose build

or just pull it with:

docker-compose pull

Using the image

To use the image, you will most likely want to create your own Dockerfile that looks something like this:

FROM alexshin/docker-django-asgi-daphne

ENV DAPHNE_PORT=8000
ENV APP_ASGI_ENTRYPOINT=proj.asgi:channel_layer
ENV DJANGO_SETTINGS_MODULE=proj.settings

COPY . ${APP_WORKDIR}

# This is only needed if daphne is going to be running behind a proxy like nginx.
CMD ["--proxy-headers"]

In this case, proj/settings.py is a file that contains the CHANNEL_LAYERS setting from your project's settings.py. What I like to do, so that my daphne image is as minimal as possible, is create a channel_settings.py in the same directory as settings.py that contains something like this:

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'asgi_redis.RedisChannelLayer',
        'ROUTING':
            'proj.routing.channel_routing'
            if 'SECRET_KEY' in os.environ
            else [],
        'CONFIG': {
            'hosts': [(os.environ.get('REDIS_HOST', 'redis'), 6379)],
        },
    },
}

# These will get overridden in settings.py but are necessary for daphne to be
# able to start.  django.setup() requires that they be set.
DEBUG = False
LOGGING = {}
SECRET_KEY = 'this can be anything except an empty string'

Then, at the top of my project's settings.py, I put:

from .channel_settings import *

This way the CHANNEL_LAYERS setting is in one place that can be used by my django project and by my daphne instance.

The important thing is that this file gets copied to /home/daphne/proj/channel_settings.py by way of the Dockerfile.

The CMD contains command-line options to give to daphne besides the default ones, which include:

-b $DAPHNE_PORT --access-log - [your options go here] $APP_ASGI_ENTRYPOINT

About

Create a base docker image for running daphne (for django channels)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dockerfile 81.5%
  • Shell 12.9%
  • Python 5.6%