django-redis connection error inside docker

萝らか妹 提交于 2021-01-28 16:34:26

问题


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

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"

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


回答1:


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.

Update your docker-compose.yml and change the REDIS_URI to reference the correct hostname:

REDIS_URI: redis://redis:6379


来源:https://stackoverflow.com/questions/53359634/django-redis-connection-error-inside-docker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!