Desiigner
Desiigner

Reputation: 2316

Deploy Django Channels with Docker

I'm trying to deploy django channels with Docker and Django doesn't seem to find Redis (which I'm using as a channel layer).

When I do it locally, I just run redis-server and point to it from settings:

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            'hosts': [('localhost', 6379)],
        },
    },
}

Everything works fine, web sockets are accepting connections and easily transfer my data. For production environment I use this docker config:

version: "3"

services:

  backend:
    container_name: backend
    restart: 'on-failure'
    image: registry.testtesttest.com/app/backend:${BACKEND_VERSION:-latest}
    ports:
      - "8000:8000"
    environment:
      DJANGO_SETTINGS_MODULE: ${DJANGO_SETTINGS_MODULE:-settings.production}
      DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY:-dev}

    redis:
        image: "redis:alpine"
        ports: 
           -"6379:6379"

And I point to redis from production settings:

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            'hosts': [('redis', 6379)],
        },
    },
}

And on production, Django says:

Cannot find redis on 127.0.0.1:6379

What am I doing wrong? Do I have to add any extra services do docker-compose file?

Upvotes: 9

Views: 4937

Answers (1)

Mahabbat Huseynov
Mahabbat Huseynov

Reputation: 11

You need to give links for backend.

backend:
    container_name: backend
    restart: 'on-failure'
    image: registry.testtesttest.com/app/backend:${BACKEND_VERSION:-latest}
    ports:
      - "8000:8000"
    environment:
      DJANGO_SETTINGS_MODULE: ${DJANGO_SETTINGS_MODULE:-settings.production}
      DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY:-dev}
    links:
     - redis

Upvotes: 1

Related Questions