Docker [Errno 111] Connect调用失败('127.0.0.1',6379) [英] Docker [Errno 111] Connect call failed ('127.0.0.1', 6379)
问题描述
我正在尝试在此处关注该教程
https:/ /channels.readthedocs.io/en/latest/tutorial/part_2.html 并检查渠道层是否可以与Redis通信。我正在做的唯一不同的事情是,我正在使用docker-compose并在docker容器上运行整个程序,这似乎使所有事情搞砸了。这是我尝试
I am trying to follow the tutorial here https://channels.readthedocs.io/en/latest/tutorial/part_2.html and check if channel layer can communicate with Redis. The only different thing I'm doing is that I'm using docker-compose and running the entire thing on a docker container, and that seems to be messing up with everything. This is the error message I'm getting when I try to
run async_to_sync(channel_layer.send)('test_channel',{'type' :'hello'})
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/asgiref/sync.py", line 116, in __call__
return call_result.result()
File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 428, in result
return self.__get_result()
File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/usr/local/lib/python3.7/site-packages/asgiref/sync.py", line 156, in main_wrap
result = await self.awaitable(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/channels_redis/core.py", line 293, in send
async with self.connection(index) as connection:
File "/usr/local/lib/python3.7/site-packages/channels_redis/core.py", line 820, in __aenter__
self.conn = await self.pool.pop()
File "/usr/local/lib/python3.7/site-packages/channels_redis/core.py", line 70, in pop
conns.append(await aioredis.create_redis(**self.host, loop=loop))
File "/usr/local/lib/python3.7/site-packages/aioredis/commands/__init__.py", line 175, in create_redis
loop=loop)
File "/usr/local/lib/python3.7/site-packages/aioredis/connection.py", line 113, in create_connection
timeout)
File "/usr/local/lib/python3.7/asyncio/tasks.py", line 414, in wait_for
return await fut
File "/usr/local/lib/python3.7/site-packages/aioredis/stream.py", line 24, in open_connection
lambda: protocol, host, port, **kwds)
File "/usr/local/lib/python3.7/asyncio/base_events.py", line 958, in create_connection
raise exceptions[0]
File "/usr/local/lib/python3.7/asyncio/base_events.py", line 945, in create_connection
await self.sock_connect(sock, address)
File "/usr/local/lib/python3.7/asyncio/selector_events.py", line 473, in sock_connect
return await fut
File "/usr/local/lib/python3.7/asyncio/selector_events.py", line 503, in _sock_connect_cb
raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('127.0.0.1', 6379)
我检查了一些帖子,发现很多建议是因为Redis没有运行。我知道Redis存在,因为docker ps显示
I've checked a few post and saw that many suggested this is because Redis isn't running. I know that Redis exist since docker ps shows that
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2ccab2cfc570 test_web "python manage.py ru…" 7 minutes ago Up 7 minutes 0.0.0.0:8000->8000/tcp test_web_1
6da398f093fc redis:2.8 "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 0.0.0.0:6379->6379/tcp relaxed_aryabhata
任何人都知道我现在能做什么吗?我真的很陌生
Any idea what I can do right now? I'm really new to these
推荐答案
尝试更改 127.0.0.1:6379
到 redis:6379
。
尽管Redis正在运行,但是您的 python
容器无法与其通信;这是因为它试图连接到 127.0.0.1:6379
,但是从容器的角度来看,那里没有任何运行。调试可能会让人感到沮丧,但是如果您要记住容器拥有自己的网络名称空间,则会更容易一些。结果是 python
的本地主机!= redis
的本地主机!=主机的 localhost
。
Although Redis is running, your python
container isn't able to communicate with it; this is because it's trying to connect to 127.0.0.1:6379
, but from the container's perspective, there's nothing running there. This can be a bit frustrating to debug, but it's a bit easier if you keep in mind that containers get their own network namespace. As a result, python
's localhost != redis
's localhost != your host machine's localhost
.
幸运的是,连接共享同一网桥的容器很容易,并且默认情况下, docker-compose
创建一个桥接网络,并将所有容器连接到它们,提供必要的DNS以使它们能够彼此发现。结果,容器到容器的通信只需使用服务名称即可。
Luckily, it's easy to connect containers that are sharing the same bridge, and by default, docker-compose
creates a single bridge network and connects all your containers to them, providing the necessary DNS to allow them to discover one another. As a result, container-to-container communication works simply by using the service name.
请注意,可以通过在同一名称空间中运行容器,并在主机的名称空间中运行它们-net =容器:< container-id>
或-net = host
标志。这对于在容器中运行调试工具并将其附加到另一个容器或主机的网络名称空间(例如,使用 netshoot 到查看容器中正在监听的端口(是否暴露), docker运行--rm -it --net容器:test_web_1 nicolaka / netshoot netstat -tulpn
。
As a note, it's possible to run containers in the same namespace, and to run in them in the namespace of the host, via the --net=container:<container-id>
or --net=host
flag. This is especially useful for running debugging tools in a container and attaching them to the network namespace of either another container or the host, e.g. using netshoot to see what ports are listening within the container (exposed or not), docker run --rm -it --net container:test_web_1 nicolaka/netshoot netstat -tulpn
.
这篇关于Docker [Errno 111] Connect调用失败('127.0.0.1',6379)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!