docker django无法访问redis容器的解决方法

yipeiwu_com6年前Python基础

docker-compose.yal文件中:

 redis:
  image: redis
  container_name: xdemo.redis
  ports:
   - 6379:6379
  restart: always
django setting.py中配置redis:

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

访问redis的时候总是报错:

redis.exceptions.ConnectionError: Error 111 connecting to 127.0.0.1:6379. Connection refused.

解决方法:将127.0.0.1设置为,docker中配置的servername, redis:

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

以上这篇docker django无法访问redis容器的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

对python中的xlsxwriter库简单分析

一、xlsxwriter 基本用法,创建 xlsx 文件并添加数据 官方文档:http://xlsxwriter.readthedocs.org/ xlsxwriter 可以操作 xls...

python代码制作configure文件示例

在lua中,一直用lua作为config文件,或承载数据的文件 - 好处是lua本身就很好阅读,然后无需额外写解析的代码,还支持在configure文件中读环境变量,条件判断等。 在lu...

python/Matplotlib绘制复变函数图像教程

python/Matplotlib绘制复变函数图像教程

今天发现sympy依赖的库mpmath里也有很多数学函数,其中也有在复平面绘制二维图的函数cplot,具体例子如下 from mpmath import * def f1(z):...

Python判断以什么结尾以什么开头的实例

如下所示: str='abcdef' print(str.endswith('f')) print(str.startswith('a')) 输出结果: True True...

python re模块findall()函数实例解析

本文研究的是re模块findall()函数的相关内容,首先看看实例代码: >>> import re >>> s = "adfad asdfas...