Соединение Docker Redis Django отклонено

Я пишу проект, используя Django REST Framework, Django, Postgres в качестве базы данных и Redis в качестве кэширования. Я хочу докеризовать свой проект. Но Redis не хочет получать доступ к соединению. Настройки Джанго:

CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379/',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    }
}

докер-compose.yml:

services:

  postgres:
    image: postgres:latest
    env_file:
      - ./src/main/.env
    volumes:
      - ./scripts/postgres:/docker-entrypoint-initdb.d

  polls:
    build: .
    volumes:
      - .:/code
    env_file:
      - ./src/main/.env
    ports:
      - "8000:8000"
    depends_on:
      - postgres
      - redis
    command: ./scripts/wait_for_it.sh

  redis:
    restart: always
    image: redis:3.2.0
    expose:
      - "6379"

Когда я запускаю команду для контейнеров, появляются следующие предупреждения:

polls_cache | 1:M 15 Aug 10:47:36.719 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
polls_cache | 1:M 15 Aug 10:47:36.720 # Server started, Redis version 3.2.0
polls_cache | 1:M 15 Aug 10:47:36.720 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
polls_cache | 1:M 15 Aug 10:47:36.720 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
polls_cache | 1:M 15 Aug 10:47:36.720 * The server is now ready to accept connections on port 6379

И когда я пытаюсь выполнить запрос GET к конечной точке, где я использую Redis для кэширования, возникает исключение:

ConnectionError at /question/top/
Error 111 connecting to 127.0.0.1:6379. Connection refused.
...

Может у кого была похожая проблема?


person Oleksii Petrushynskyi    schedule 15.08.2018    source источник


Ответы (1)


Измените строку подключения ниже -

CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': 'redis://redis:6379/',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    }
}

Для сервисного контейнера опросов 127.0.0.1 — это сам контейнер опросов. При использовании docker compose контейнеры всегда доступны, используя их служебные имена, такие как redis polls postgres.

person vivekyad4v    schedule 15.08.2018
comment
То есть измените 127.0.0.1 в ключе LOCATION, чтобы он указывал на имя хоста контейнера Redis, redis. - person AKX; 15.08.2018