【问题标题】:Docker Redis Django connection refusedDocker Redis Django 连接被拒绝
【发布时间】:2019-01-22 06:14:23
【问题描述】:

我正在使用 Django REST Framework、Django、Postgres 作为数据库和 Redis 作为缓存来编写项目。我想码头化我的项目。 但是 Redis 不想访问连接。 Django 设置:

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

docker-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

当我尝试向使用 Redis 进行缓存的端点发出 GET 请求时,出现异常:

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

也许有人遇到过类似的问题?

【问题讨论】:

    标签: django docker redis docker-compose django-cache


    【解决方案1】:

    将连接字符串更改为以下 -

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

    对于 Polls 服务容器,127.0.0.1 是 polls 容器本身。在使用 docker compose 时,始终可以通过使用它们的服务名称来访问它们,例如 redis polls postgres

    【讨论】:

    • 即把LOCATION键中的127.0.0.1改成指向Redis容器的主机名redis
    猜你喜欢
    • 2017-07-10
    • 2016-02-14
    • 2015-12-13
    • 2021-01-24
    • 2021-09-18
    • 1970-01-01
    • 2021-07-17
    • 2017-05-09
    • 2021-10-22
    相关资源
    最近更新 更多