TimeoutError: [Errno 110] Connect call failed when trying to connect to redis service in docker

醉酒当歌 提交于 2021-02-11 12:26:04

问题


I'm dockerizing a web application that uses Django Channels for websocket functionality. It depends on redis. I'm having trouble getting the redis portion of the application to work correctly. I've tried to stick to this redis/compose guide.

docker-compose.yml

version: "3"

services:
  db:
    image: mysql:latest
    volumes:
      - "./.mysql-data/db:/var/lib/mysql"
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: database
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    networks:
      - hostnetwork
  backend:
    build: ./backend
    command: daphne -b 0.0.0.0 -p 8000 main.asgi:application
    volumes:
      - ./backend:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
    links:
      - redis
  redis:
    image: "redis:alpine"
    command: redis-server
    ports:
      - "6379:6379"
    volumes:
      - ./redis-data:/var/lib/redis <-- This dir doesn't exist on host machine
      - ./redis.conf:/usr/local/etc/redis/redis.conf <-- I created this file manually on host machine
    environment:
      - REDIS_REPLICATION_MODE=master
    networks:
      node_net:
        ipv4_address: 172.28.1.4
  frontend:
    build: ./frontend
    ports:
      - "3000:3000"
    volumes:
      - ./frontend:/usr/src/app
    stdin_open: true

networks:
  hostnetwork:
    external: true
  node_net:
    ipam:
      driver: default
      config:
        - subnet: 172.28.0.0/16

settings.py

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [
                (os.environ.get('REDIS_HOST', "172.28.1.4"),
                 os.environ.get('REDIS_PORT', 6379))
            ],
        },
    },
}

With the above configuration, my containers start up and and redis doesn't give any errors at first, but the websocket consumers never get or send any messages. After a minute or so, it gives the following error:

backend_1   |   File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 494, in sock_connect
backend_1   |     return await fut
backend_1   |   File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 526, in _sock_connect_cb
backend_1   |     raise OSError(err, f'Connect call failed {address}')
backend_1   | TimeoutError: [Errno 110] Connect call failed ('172.28.1.4', 6379)

What's causing the TimeoutError and the apparent lack of communication between my server and redis? How can I get the two talking?


回答1:


This answer gave me the understanding I needed to fix my problem. I simply removed:

    networks:
      node_net:
        ipv4_address: 172.28.1.4

And changed CHANNEL_LAYERS to this:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [
                (os.environ.get('REDIS_HOST', "redis"), <---
                 os.environ.get('REDIS_PORT', 6379))
            ],
        },
    },
}

I guess this allowed my services to communicate through the bridge network that docker-compose creates.



来源:https://stackoverflow.com/questions/62239449/timeouterror-errno-110-connect-call-failed-when-trying-to-connect-to-redis-se

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