【问题标题】:docker-compose + django + redis - Error 111 connecting to 127.0.0.1:6379. Connection refuseddocker-compose + django + redis - 连接到 127.0.0.1:6379 时出现错误 111。拒绝连接
【发布时间】:2019-11-28 16:50:07
【问题描述】:

我查看了this answer,但不知道如何将其应用于我的问题,因此如果有答案,我们将不胜感激。

我也是 docker 和 docker-compose 的菜鸟。

我有一个简单的 docker-compose.yml

version: '3'

services:
  redis:
    image: "redis:alpine"
  web:
    build: . # current directory
    command: bash -c "python /app/src/manage.py migrate && 
                      python /app/src/manage.py runserver 0.0.0.0:8000"
    volumes: 
      - .:/app
    ports:
      - "8000:8000"

当我运行这个时:docker-compose up 一切似乎都正常:

$ docker-compose up
Starting hackerspace_redis_1 ... 
Starting hackerspace_redis_1 ... done
Starting hackerspace_web_1 ... 
Starting hackerspace_web_1 ... done
Attaching to hackerspace_redis_1, hackerspace_web_1
redis_1  | 1:C 19 Jul 2019 16:49:10.644 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 19 Jul 2019 16:49:10.644 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 19 Jul 2019 16:49:10.644 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1  | 1:M 19 Jul 2019 16:49:10.645 * Running mode=standalone, port=6379.
redis_1  | 1:M 19 Jul 2019 16:49:10.645 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1  | 1:M 19 Jul 2019 16:49:10.645 # Server initialized
redis_1  | 1:M 19 Jul 2019 16:49:10.645 # 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.
redis_1  | 1:M 19 Jul 2019 16:49:10.645 # 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.
redis_1  | 1:M 19 Jul 2019 16:49:10.645 * DB loaded from disk: 0.000 seconds
redis_1  | 1:M 19 Jul 2019 16:49:10.645 * Ready to accept connections
web_1    | Operations to perform:
web_1    |   Apply all migrations: account, admin, announcements, auth, badges, comments, contenttypes, courses, django_summernote, djconfig, djcytoscape, flatpages, notifications, portfolios, prerequisites, profile_manager, quest_manager, sessions, sites, socialaccount, suggestions, utilities
web_1    | Running migrations:
web_1    |   No migrations to apply.
web_1    | Performing system checks...
web_1    | 
web_1    | System check identified no issues (0 silenced).
web_1    | July 19, 2019 - 09:49:16
web_1    | Django version 2.0.13, using settings 'hackerspace_online.settings'
web_1    | Starting development server at http://0.0.0.0:8000/
web_1    | Quit the server with CONTROL-C.

我可以通过浏览器在 127.0.0.0:8000 访问我的 django 应用程序。但是,当我尝试登录该应用程序时,我得到:

/accounts/login/ 处的连接错误

连接到 127.0.0.1:6379 时出现错误 111。连接被拒绝。

这是我在 django 设置中连接到 redis 的方式:

REDIS_HOST = os.environ.get('REDIS_HOST', '127.0.0.1')
REDIS_PORT = os.environ.get('REDIS_PORT', '6379')

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://{}:{}/1".format(REDIS_HOST, REDIS_PORT),
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
    #...
}

如何让我的 django 应用连接到 redis 容器?

请注意,据我所知,没有其他使用端口 6379(通过sudo lsof -i -P -n | grep LISTEN

【问题讨论】:

  • REDIS_HOST 的值是多少?

标签: django docker redis docker-compose


【解决方案1】:

127.0.0.1 在 Docker 中几乎总是意味着“这个容器”。如果你在Docker Compose 下运行你的应用程序,它

... 为您的应用设置单一网络。服务的每个容器都加入默认网络,并且该网络上的其他容器可访问,并且它们可以通过与容器名称相同的主机名发现

也就是说,在这个docker-compose.yml 文件的上下文中,有主机名redisweb 指向两个容器。

您已经完成了一项重要的配置工作。当你的代码说

REDIS_HOST = os.environ.get('REDIS_HOST', '127.0.0.1')

您可以设置一个覆盖内置默认值的环境变量。所以在这里你只需要在你的docker-compose.yml文件中添加一个环境变量设置:

version: '3'
services:
  redis:
    image: "redis:alpine"
  web:
    build: . # current directory
    environment:
      - REDIS_HOST=redis
    ports:
      - "8000:8000"
    # application source code and default command are built into the image

【讨论】:

  • 我们可以使用 REDIS_HOST = os.environ.get('REDIS_HOST', 'redis')
  • 我的一般经验是,在 Docker 或 Kubernetes 或云提供商环境中,您可以轻松设置环境变量,默认值无关紧要。如果没有设置环境变量,那是因为它是开发人员在本地运行代码,在 Docker 之外,在虚拟环境中,localhost 可能是正确的默认值。
【解决方案2】:

如果您使用 docker-compose,您需要引用容器的名称(即 redis)而不是 localhost。本地主机是每个容器,docker-compose 将连接隐式网络上的每个容器,它们需要为其解析名称。将它们视为独立的机器。它有助于在配置网络时获得透视。因此,您尝试连接到 redis 的快速调整应该可以解决问题。

文档https://docs.docker.com/compose/networking/ 描述了在您不自己明确创建默认网络时如何创建它们。

【讨论】: