Docker Redis Django连接被拒绝

时间:2018-08-15 11:03:38

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

我正在使用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.
...

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

1 个答案:

答案 0 :(得分:5)

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

$("remote_id").click(function(event){
    event.preventDefault()
    $.ajax("http://externalresourceurl.com", {
      success: function(data) {
         // fill each form field with corresponding item in the data object
         $('#first_name').html($(data).first_name);
         ...
      },
      error: function() {
         $('#notification-bar').text('An error occurred');
      }
    }
 );
});

对于民意调查服务容器,CACHES = { 'default': { 'BACKEND': 'django_redis.cache.RedisCache', 'LOCATION': 'redis://redis:6379/', 'OPTIONS': { 'CLIENT_CLASS': 'django_redis.client.DefaultClient', } } } 是民意调查容器本身。在使用docker compose容器时,始终可以通过使用其服务名(例如127.0.0.1 redis polls来访问它们。)