Celery工作人员无法连接到Docker实例上的Redis [英] Celery workers unable to connect to redis on docker instances
问题描述
我有一个运行Django应用程序的dockerized设置,其中使用了Celery任务。 Celery使用Redis作为代理。
I have a dockerized setup running a Django app within which I use Celery tasks. Celery uses Redis as the broker.
版本控制:
- Docker版本17.09.0-ce,构建afdb6d4
- docker-compose版本1.15.0,构建e12f3b9
- Django == 1.9。 6
- django-celery-beat == 1.0.1
- celery == 4.1.0
- celery [redis]
- redis == 2.10.5
- Docker version 17.09.0-ce, build afdb6d4
- docker-compose version 1.15.0, build e12f3b9
- Django==1.9.6
- django-celery-beat==1.0.1
- celery==4.1.0
- celery[redis]
- redis==2.10.5
问题:
我的芹菜工人似乎无法连接到位于localhost:6379的redis容器。我可以在指定端口上远程登录到Redis服务器。我能够验证redis服务器是否在容器上运行。
My celery workers appear to be unable to connect to the redis container located at localhost:6379. I am able to telnet into the redis server on the specified port. I am able to verify redis-server is running on the container.
当我手动连接到Celery docker实例并尝试使用命令<$ c创建工作线程时, $ c>芹菜-后端工作人员-l信息我得到通知:
When I manually connect to the Celery docker instance and attempt to create a worker using the command celery -A backend worker -l info
I get the notice:
[2017-11-13 18:07:50,937:[ERROR / MainProcess]使用者:无法连接至redis:// localhost:6379/0:错误99连接至localhost:6379。无法分配请求的地址。
在4.00秒后重试...
注意:
我能够通过telnet进入端口6379上的redis容器。在redis容器上,redis-server正在运行。
I am able to telnet in to the redis container on port 6379. On the redis container, redis-server is running.
还有其他我想念的东西吗?我已经走了很短的距离,但是感觉好像缺少了一些非常简单的东西。
Is there anything else that I'm missing? I've gone pretty far down the rabbit hole, but feel like I'm missing something really simple.
DOCKER配置文件:
docker-compose.common.yml 此处 < br>
docker-compose.dev.yml 此处
docker-compose.common.yml here
docker-compose.dev.yml here
推荐答案
使用docker-compose时,不会在容器间使用 localhost
通信时,您将使用容器的组合分配主机名。在这种情况下,您的redis容器的主机名是 redis
。 服务:
下的顶级元素是您的默认主机名。
When you use docker-compose, you aren't going to be using localhost
for inter-container communication, you would be using the compose-assigned hostname of the container. In this case, the hostname of your redis container is redis
. The top level elements under services:
are your default host names.
因此,要使芹菜连接到Redis,您应该尝试 redis:// redis:6379/0
。由于协议和服务名称相同,因此我将详细说明:如果您在docker-compose中将redis服务命名为 butter-pecan-redis,则应使用 redis: // butter-pecan-redis:6379/0
。
So for celery to connect to redis, you should try redis://redis:6379/0
. Since the protocol and the service name are the same, I'll elaborate a little more: if you named your redis service "butter-pecan-redis" in your docker-compose, you would instead use redis://butter-pecan-redis:6379/0
.
此外,docker-compose.dev.yml似乎没有芹菜和在公共网络上重做,这可能会导致它们彼此之间无法看到对方。我相信他们需要共享至少一个公共网络才能解析各自的主机名。
Also, docker-compose.dev.yml doesn't appear to have celery and redis on a common network, which might cause them not to be able to see each other. I believe they need to share at least one network in common to be able to resolve their respective host names.
docker-compose中的网络在前几段中都有一个示例,其中有一个docker-compose.yml。
Networking in docker-compose has an example in the first handful of paragraphs, with a docker-compose.yml to look at.
这篇关于Celery工作人员无法连接到Docker实例上的Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!