docker 中的 django-redis 连接错误 [英] django-redis connection error inside docker
问题描述
django 视图.py
django views.py
import redis
import jwt
from access import utils
import os
redis_url = os.environ['REDIS_URI']
R = redis.StrictRedis(redis_url)
def set(request):
R.set('foo', 'bar')
return JsonResponse({"code":200,"msg":"success"})
docker-compose
docker-compose
version: "3"
services:
rango:
container_name: rango
build: ./
command: python backend/manage.py runserver 0.0.0.0:8000
# command: npm start --prefix frontend/rango-frontend/
working_dir: /usr/src/rango
environment:
REDIS_URI: redis://redis_db:6379
ports:
- "8000:8000"
tty: true
links:
- elasticsearch
- node
- redis
#elastic search
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.5.0
ports:
- "9200:9200"
#node
node:
image: node:10.13.0
#redis
redis:
image: redis
environment:
- ALLOW_EMPTY_PASSWORD=yes
ports:
- "6379:6379"
在这里,我从 docker 内部的 django 连接 redis.它给了我拒绝连接的例外.请查看我的代码并在下面分享截图
here i am connecting redis from django inside docker. it is giving me exceptions connexctions refused. Please have a look into my code and shared screenshot below
推荐答案
默认情况下,docker compose 使用与容器名称相同的主机名使容器可被发现.因此,您的 redis 容器可以通过主机名 redis
发现.但是,您的 Django 容器使用的是主机名 redis_db
.
By default, docker compose makes containers discoverable with a hostname identical to the container name. Your redis container is thus discoverable via the hostname redis
. However, your Django container is using the hostname redis_db
.
更新您的 docker-compose.yml
并更改 REDIS_URI
以引用正确的主机名:
Update your docker-compose.yml
and change the REDIS_URI
to reference the correct hostname:
REDIS_URI: redis://redis:6379
这篇关于docker 中的 django-redis 连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!