1

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

enter image description here

3
  • Welcome to Stack Overflow. In addition to posting the code, please be sure to ask a clear question. How to Ask. Thanks. Commented Nov 18, 2018 at 10:00
  • Could you show your Redis configuration in settings.py? Commented Nov 18, 2018 at 10:51
  • i am not using redis with client. not in settinggs.py Commented Nov 18, 2018 at 11:50

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

9 Comments

in python what changes i need to do.
No changes in python. You just need to update your docker-compose.yml and change the REDIS_URI to redis://redis:6379
and do i nee to start redis inside the docker or it will automatically start. just like in local system we need to start redis server before working on it using src/redis-server
Redis will come up when the container starts. You don't need to enter the container to start it.
rango | redis.exceptions.ConnectionError: Error -2 connecting to redis://redis:6379:6379. Name or service not known. showing error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.