时间:2021-11-19 13:21:25 | 栏目: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": "",
},
},
}