我有一个 django
使用的应用程序 channels
, channels_redis
以及 graphene_subscriptions
. graphene_subscriptions
用于通过发布消息 channels
保存数据库模型示例时( DATABASE_MODEL_INSTANCE.save()
).
django应用程序和 redis
(当然还有fullstack应用程序的其他部分)是使用单独的docker容器运行的 docker-compose
. 当我运行安装程序时 docker-compose
在linux上的网络模式主机中,一切都很好。但是,如果使用自定义网络(一个用于后端,一个用于前端)重新配置网络模式网桥的设置,并使用 docker-compose
我得到以下错误:
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
我的开发配置如下所示。
节选 settings.py
文件:
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("redis", 6379)],
},
},
}
节选 redis.conf
文件:
# bind 127.0.0.1
protected-mode no
节选 nginx.conf
文件:
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;
}
}
节选 docker-compose.yml
文件:
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
配置过程中我遗漏了什么?
1条答案
按热度按时间njthzxwz1#
似乎docker安装程序没有关闭
docker-compose -f docker-compose.yml down
正确地。通过运行完成清理后docker network prune
,docker image prune
,docker container prune
并用docker-compose -f docker-compose.yml build
然后重新启动docker-compose -f docker-compose.yml up
一切正常。