简体   繁体   中英

channels_redis raises "Name or service not known" in docker-compose network mode bridge

I've a django app which uses channels , channels_redis and graphene_subscriptions . graphene_subscriptions is used to publish a message via channels when a database model instance is saved ( DATABASE_MODEL_INSTANCE.save() ).

The Django app and redis (as well as the other parts of the fullstack app of course) is run separate docker containers using docker-compose . When I run the setup with docker-compose in network mode host on Linux everything is just fine. However if reconfigure the setup for network mode bridge with custom networks (one for the backend, one for the frontend) and run the setup with docker-compose I get the following error:

c_backend    |   File "./APP_SPECIFIC_PATH/FILE.py", line X, in FUNCTION
c_backend    |     DATABASE_MODEL_INSTANCE.save()
c_backend    |   File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 746, in save
c_backend    |     force_update=force_update, update_fields=update_fields)
c_backend    |   File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 795, in save_base
c_backend    |     update_fields=update_fields, raw=raw, using=using,
c_backend    |   File "/usr/local/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in send
c_backend    |     for receiver in self._live_receivers(sender)
c_backend    |   File "/usr/local/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in <listcomp>
c_backend    |     for receiver in self._live_receivers(sender)
c_backend    |   File "/usr/local/lib/python3.7/site-packages/graphene_subscriptions/signals.py", line 15, in post_save_subscription
c_backend    |     event.send()
c_backend    |   File "/usr/local/lib/python3.7/site-packages/graphene_subscriptions/events.py", line 20, in send
c_backend    |     "subscriptions", {"type": "signal.fired", "event": self.to_dict()}
c_backend    |   File "/usr/local/lib/python3.7/site-packages/asgiref/sync.py", line 116, in __call__
c_backend    |     return call_result.result()
c_backend    |   File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 428, in result
c_backend    |     return self.__get_result()
c_backend    |   File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
c_backend    |     raise self._exception
c_backend    |   File "/usr/local/lib/python3.7/site-packages/asgiref/sync.py", line 156, in main_wrap
c_backend    |     result = await self.awaitable(*args, **kwargs)
c_backend    |   File "/usr/local/lib/python3.7/site-packages/channels_redis/core.py", line 614, in group_send
c_backend    |     async with self.connection(self.consistent_hash(group)) as connection:
c_backend    |   File "/usr/local/lib/python3.7/site-packages/channels_redis/core.py", line 835, in __aenter__
c_backend    |     self.conn = await self.pool.pop()
c_backend    |   File "/usr/local/lib/python3.7/site-packages/channels_redis/core.py", line 73, in pop
c_backend    |     conns.append(await aioredis.create_redis(**self.host, loop=loop))
c_backend    |   File "/usr/local/lib/python3.7/site-packages/aioredis/commands/__init__.py", line 175, in create_redis
c_backend    |     loop=loop)
c_backend    |   File "/usr/local/lib/python3.7/site-packages/aioredis/connection.py", line 113, in create_connection
c_backend    |     timeout)
c_backend    |   File "/usr/local/lib/python3.7/asyncio/tasks.py", line 414, in wait_for
c_backend    |     return await fut
c_backend    |   File "/usr/local/lib/python3.7/site-packages/aioredis/stream.py", line 24, in open_connection
c_backend    |     lambda: protocol, host, port, **kwds)
c_backend    |   File "/usr/local/lib/python3.7/asyncio/base_events.py", line 909, in create_connection
c_backend    |     type=socket.SOCK_STREAM, proto=proto, flags=flags, loop=self)
c_backend    |   File "/usr/local/lib/python3.7/asyncio/base_events.py", line 1286, in _ensure_resolved
c_backend    |     proto=proto, flags=flags)
c_backend    |   File "/usr/local/lib/python3.7/asyncio/base_events.py", line 788, in getaddrinfo
c_backend    |     None, getaddr_func, host, port, family, type, proto, flags)
c_backend    |   File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
c_backend    |     result = self.fn(*self.args, **self.kwargs)
c_backend    |   File "/usr/local/lib/python3.7/socket.py", line 752, in getaddrinfo
c_backend    |     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
c_backend    | socket.gaierror: [Errno -2] Name or service not known

My development configuration looks like below.

Excerpt of settings.py file:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("redis", 6379)],
        },
    },
}

Excerpt of redis.conf file:

#bind 127.0.0.1
protected-mode no

Excerpt of nginx.conf file:

upstream django {
  server django:8080;
}

server {
  listen 8000;
  listen [::]:8000;

  location /graphql/subscriptions {
    proxy_pass http://django/graphql/subscriptions;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $server_name;
  }
}

Excerpt of docker-compose.yml file:

version: "3.5"
services:
  redis:
    image: redis:5.0.7-alpine
    hostname: redis
    container_name: c_redis
    networks:
      - nw_backend
    ports:
      - "6379:6379"
    volumes:
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf:ro
    command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
  nginx:
    image: nginx:1.17.9-alpine
    hostname: nginx
    container_name: c_nginx
    networks:
      - nw_frontend
      - nw_backend
    expose:
      - "80"
      - "8000"
    ports:
      - "80:8000"
      - "8000:8000"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/conf.d/nginx.conf:ro
    depends_on:
      - django
  django:
    hostname: django
    container_name: c_django
    ports:
      - "8080:8080"
    depends_on:
      - redis
      - database
    networks:
      - nw_frontend
      - nw_backend
networks:
  nw_frontend:
    driver: bridge
    name: n_frontend
  nw_backend:
    driver: bridge
    name: n_backend

What have I missed during configuration?

Seems like the docker setup did not shutdown with docker-compose -f docker-compose.yml down correctly. After complete cleanup by running docker network prune , docker image prune , docker container prune and rebuild with docker-compose -f docker-compose.yml build and restart with docker-compose -f docker-compose.yml up everything worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM