docker-compose + django + redis - 连接到 127.0.0.1:6379 时出现错误 111。连接被拒绝

2023-11-23

我已经看过了这个答案并且不知道如何将其应用于我的问题,所以如果答案存在,我们将不胜感激。

我也是 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.

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

/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)


127.0.0.1Docker 中几乎总是意味着“这个容器”。如果您正在运行您的应用程序Docker 组合, it

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

也就是说,在这个背景下docker-compose.yml文件中,有主机名redis and web那个点在两个容器上。

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

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

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

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

docker-compose + django + redis - 连接到 127.0.0.1:6379 时出现错误 111。连接被拒绝 的相关文章

随机推荐

  • 在...中找不到 JNI_OnLoad 跳过 init

    我在过去一天左右就收到了这条消息 到目前为止还没有造成任何问题 我早些时候让我的代码与我的本机库一起运行 但今天我添加了一些新功能 但它再次无法工作 LogCat 中没有显示任何错误 但我的程序只是在我的设备上立即打开和关闭 没有任何消息表
  • Jquery 移动粘性页脚

    我想要 Jquery Mobile 中的页脚 它不是固定的 但始终位于页面底部 像这样 http ryanfait com sticky footer 但在 JQuery Mobile 中 不像标准的 JQuery Mobile 固定页脚
  • 如何捕获滚动事件?

    我想实现无限滚动 下面是我的布局的简短形式 由于我有一些元素相对定位 因此 javascript 滚动事件不会触发 如何解决这个问题以便触发滚动事件并实现无限滚动 我的主要布局是 div div class wrapper div div
  • 如何获取站点的物理路径属性

    当我仅列出具有默认格式的站点时 它会显示物理路径 PS C Windows system32 gt sm Get IISServerManager PS C Windows system32 gt sm Sites Name ID Stat
  • 如何获取 WebKit 中所有受支持的 CSS 属性?

    在 Firefox Opera 和 IE 中我可以通过以下方式获取它们 gt gt for k in document body style console log k gt opacity background height textAl
  • 禁用列表视图的滚动并启用整个布局

    您好 我目前正在开发一个 Android 应用程序 它在主活动中有两个列表视图 我想要的是禁用两个列表的滚动并仅允许整个页面滚动 有什么方法可以帮助 我的代码 包com example listviewdemo import android
  • 禁用 Rails 中的资源预编译功能

    我想禁用 Rails 中的资产预编译功能 我正在开发用户可以下载代码的应用程序 我想避免更改下载的 html 文件中的 css 和 js 文件的名称 以便用户可以运行 index html 并查看所有内容 这是该应用程序 http impr
  • 在 Eclipse-CDT 中使用*相对*路径引用第 3 方库

    使用以下方式引用第 3 方 C 组件 例如包含 库 的最佳实践是什么relativeEclipse CDT 中的路径 我正在尝试参考 Boost 和 Google protobuf 我已将它们分别放在工作区的一个文件夹中 例如 home 用
  • 以编程方式设置按钮的边距

    我是 android 新手 陷入了一个非常基本的问题 我正在开发一个应用程序 其中我需要在 fling 上滑动图像 在每个图像上 我必须动态添加按钮 我正在使用 AddContentView 添加按钮来添加按钮一切工作正常 但我想动态设置按
  • 从客户端取消通过 WCF 长时间运行的任务

    我将 WCF 服务设置为 PerCall 我想知道如何从客户端发送 Start 调用来启动长时间运行的进程 并发送 Cancel 命令来取消它 我的 WCF 服务看起来像这样 ServiceBehavior InstanceContextM
  • KeyError 中错误消息的新行 - Python 3.3

    我通过 IDLE 使用 Python 3 3 运行如下代码时 raise KeyError This is a n Line break 它输出 Traceback most recent call last File test py li
  • LNK1117:选项“VERSION:1.0.0”中的语法错误[重复]

    这个问题在这里已经有答案了 当我尝试构建时 我有一个非常好奇的问题Visual Studio 2013 有人可以帮助我吗 我做错了什么 原始消息 德语 1 gt Erstellen gestartet Projekt npworpg Kon
  • Scala 类型:A 类不等于 T,其中 T 为:类型 T = A

    我正在阅读 Scala 编程 一书的第 20 7 节 我想知道为什么这段代码编译时 class Food class Fish extends Food class Grass extends Food abstract class Ani
  • Logback - 启动时不创建空日志文件

    我有一个项目 其中有很多 工具 类 它们有自己的日志记录 这些日志文件是在应用程序启动时创建的 但在使用之前保持为空 是否可以告诉 logback 在启动时不应创建空文件 但只有当它们被使用时 不知何故 我找不到有关此主题的信息 谢谢 Lo
  • Xamarin.Forms - 将 CollectionView 的高度调整为适合儿童的最小尺寸

    I have a CollectionView with a few items in it I m trying to adjust the size of the CollectionView to be just big enough
  • 如何使用Python读取MS-Word文件中表格的内容?

    如何读取和处理 DOCX 文件中表格的每个单元格的内容 我在 Windows 7 和 PyWin32 上使用 Python 3 2 来访问 MS Word 文档 我是初学者 所以我不知道到达表格单元格的正确方法 到目前为止我刚刚这样做了 i
  • 如何在Android中检查特定设备是否支持4G网络?

    我想检查一下是否特定设备具有对 4G 网络的硬件支持 我将详细说明这个问题 在应用程序中 我们有一个设置页面 用户可以在其中进行选择并允许应用程序仅在选定的网络中运行 例如 用户可以选择该应用程序仅在 WiFi 网络或仅在 3G 网络等中运
  • webapi2 返回不带引号的简单字符串

    简单场景 public IHttpActionResult Get return Ok
  • ClosureCleaner.clean的目的

    Before sc runJob调用dagScheduler runJob 对 rdd 执行的 func 被 清理 为ClosureCleaner clean 为什么 Spark 必须这样做 目的是什么 Ankur Dave 一位 Spar
  • docker-compose + django + redis - 连接到 127.0.0.1:6379 时出现错误 111。连接被拒绝

    我已经看过了这个答案并且不知道如何将其应用于我的问题 所以如果答案存在 我们将不胜感激 我也是 docker 和 docker compose 的菜鸟 我有一个简单的 docker compose yml version 3 service