docker

时间:2018-11-18 09:54:02

标签: django redis

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"

在这里,我正在从Django内部的Django连接redis。 它给了我例外,拒绝连接。 请在下面查看我的代码和共享的屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:1)

默认情况下,docker compose使可发现的容器的主机名与容器名称相同。因此,可以通过主机名redis发现您的redis容器。但是,您的Django容器正在使用主机名redis_db

更新您的docker-compose.yml并更改REDIS_URI以引用正确的主机名:

REDIS_URI: redis://redis:6379