从一个 Docker 容器连接到另一个 [英] Connect from one Docker container to another

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

问题描述

我想在一个 docker 容器中运行 rabbitmq-server 并使用 celery (http://celeryproject.组织/)

I want to run rabbitmq-server in one docker container and connect to it from another container using celery (http://celeryproject.org/)

我使用以下命令运行 rabbitmq...

I have rabbitmq running using the below command...

sudo docker run -d -p :5672 markellul/rabbitmq /usr/sbin/rabbitmq-server

并通过

sudo docker run -i -t markellul/celery /bin/bash

当我尝试做非常基本的教程来验证 http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html

When I am trying to do the very basic tutorial to validate the connection on http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html

我收到连接被拒绝错误:

I am getting a connection refused error:

消费者:无法连接到 amqp://guest@127.0.0.1:5672//:[Errno 111]连接被拒绝.

consumer: Cannot connect to amqp://guest@127.0.0.1:5672//: [Errno 111] Connection refused.

当我将 rabbitmq 安装在与 celery 相同的容器上时,它工作正常.

When I install rabbitmq on the same container as celery it works fine.

我需要做什么才能让容器相互交互?

What do I need to do to have container interacting with each other?

推荐答案

[edit 2016]

直接链接现已弃用.进行链接容器的新方法是 docker network connect.它的工作原理与虚拟网络非常相似,并且比旧的链接方式具有更广泛的功能集.

[edit 2016]

Direct links are deprecated now. The new way to do link containers is docker network connect. It works quite similar to virtual networks and has a wider feature set than the old way of linking.

首先创建命名容器:

docker run --name rabbitmq -d -p :5672 markellul/rabbitmq /usr/sbin/rabbitmq-server
docker run --name celery -it markellul/celery /bin/bash

然后你创建一个网络(最后一个参数是你的网络名称):

Then you create a network (last parameter is your network name):

docker network create -d bridge --subnet 172.25.0.0/16 mynetwork

将容器连接到新创建的网络:

Connect the containers to your newly created network:

docker network connect mynetwork rabbitmq
docker network connect mynetwork celery

现在,两个容器都在同一个网络中,可以相互通信了.

Now, both containers are in the same network and can communicate with each other.

可以在 找到非常详细的用户指南使用网络:连接容器.

有一个 Docker 0.6.5 中的新特性叫做链接,旨在帮助 docker 容器之间的通信.

There is a new feature in Docker 0.6.5 called linking, which is meant to help the communication between docker containers.

首先,像往常一样创建您的 rabbitmq 容器.请注意,我还使用了新的名称"功能,这让生活变得更轻松了一点:

First, create your rabbitmq container as usual. Note that i also used the new "name" feature which makes life a litte bit easier:

docker run --name rabbitmq -d -p :5672 markellul/rabbitmq /usr/sbin/rabbitmq-server

你可以使用link参数来映射一个容器(我们这里用名字,id也可以):

You can use the link parameter to map a container (we use the name here, the id would be ok too):

docker run --link rabbitmq:amq -i -t markellul/celery /bin/bash

现在你可以访问rabbitmq容器的IP和Port,因为docker自动添加了一些环境变量:

Now you have access to the IP and Port of the rabbitmq container because docker automatically added some environmental variables:

$AMQ_PORT_5672_TCP_ADDR
$AMQ_PORT_5672_TCP_PORT

此外,Docker 将源容器的主机条目添加到 /etc/hosts 文件中.在此示例中,amq 将是容器中定义的主机.
来自 Docker 文档:

In addition Docker adds a host entry for the source container to the /etc/hosts file. In this example amq will be a defined host in the container.
From Docker documentation:

与/etc/hosts 文件中的主机条目不同,如果源容器重新启动,存储在环境变量中的 IP 地址不会自动更新.我们建议使用/etc/hosts 中的主机条目来解析链接容器的 IP 地址.

Unlike host entries in the /etc/hosts file, IP addresses stored in the environment variables are not automatically updated if the source container is restarted. We recommend using the host entries in /etc/hosts to resolve the IP address of linked containers.

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

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