Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 884
Desiigner Avatar asked Mar 27 '19 19:03

Desiigner


1 Answers

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
like image 126
Mahabbat Huseynov Avatar answered Sep 18 '22 12:09

Mahabbat Huseynov