从另一个容器中的服务连接到Rabbitmq Docker容器 [英] Connecting to rabbitmq docker container from service in another container

查看:321
本文介绍了从另一个容器中的服务连接到Rabbitmq Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了很多搜索,但无法解决此问题。

I've done a lot of searching but I cannot fix this issue.

我有一个通过以下命令运行的基本Rabbitmq容器:

I have a basic Rabbitmq container running via this command:

docker run- d --hostname rabbitmqhost --name rabbitmq -p 15672:15672 -p 5672:5672 rabbitmq:3-management

我正在使用 nameko 创建一个连接到此容器的微服务。这是基本的微服务模块 main.py

I am using nameko to create a microservice which connects to this container. Here's a basic microservice module main.py:

from nameko.rpc import rpc
class Service_Name(object):
    name = "service_name"

    @rpc
    def service_endpoint(self, arg=None):
        logging.info('service_one endpoint, arg = %s', arg)

此服务运行并连接到

nameko run main --broker amqp:// guest:guest @ localhost

我想将服务放入Docker容器(称为 service_one ),但是当我这样做时并运行先前的nameko命令,我将得到 socket.error:[Errno 111] ECONNREFUSED 不管我如何尝试链接两个容器。

I wanted to put the service into a Docker container (called service_one) but when I do so and run the previous nameko command I get socket.error: [Errno 111] ECONNREFUSED no matter how I try and link the two containers.

正确的方法是什么?目的是将每个服务都放在一个容器中,所有服务都通过兔子相互通信。谢谢。

What would be the correct method? The aim is to have each service in a container, all talking to each other through rabbit. Thanks.

推荐答案

如果您正在容器中运行服务,则 amqp:// guest :guest @ localhost 对您没有任何好处; localhost 是指容器的网络名称空间...因此,您当然会得到 ECONNREFUSED ,因为没有人在听

If you're running a service inside a container, then amqp://guest:guest@localhost won't do you any good; localhost refers to the network namespace of the container...so of course you get an ECONNREFUSED, because there's nothing listening there.

如果要连接到另一个容器中的服务,则需要使用该容器的ip地址或解析为ip地址的主机名。

If you want to connect to a service in another container, you need to use the ip address of that container, or a hostname that resolves to the ip address of that container.

如果您在用户定义的网络,然后Docker维护一个DNS服务器,该服务器会将容器名称映射到地址。也就是说,如果我首先创建一个网络:

If you are running your containers in a user-defined network, then Docker maintains a DNS server that will map container names to addresses. That is, if I first create a network:

docker network create myapp_net

,然后在该网络中启动Rabbitmq容器:

And then start a rabbitmq container in that network:

docker run -d --network myapp_net --hostname rabbitmqhost \
   --name rabbitmq -p 15672:15672 -p 5672:5672 rabbitmq:3-management

然后在该网络中启动的 other 容器将能够使用主机名 rabbitmq 以连接到该容器。

Then other containers started in that network will be able to use the hostname rabbitmq to connect to that container.

对于在默认网络中运行的容器(不使用-network 参数命令行),则可以使用-link 选项来实现类似的效果,但灵活性较差,如此处

For containers running in the default network (no --network parameter on the command line), you can use the --link option to achieve a similar, though less flexible, effect, as documented here.

这篇关于从另一个容器中的服务连接到Rabbitmq Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆