从一个Docker容器连接到另一个 [英] Connect from one Docker container to another
问题描述
我想在一个docker容器中运行Rabbitmq-server并使用celery从另一个容器连接到它( http:// celeryproject。 org / )
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与芹菜安装在同一容器上时,效果很好。
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?
推荐答案
现在不建议使用直接链接。链接容器的新方法是 docker网络连接。它的工作原理与虚拟网络非常相似,并且具有比旧的链接方式更广泛的功能。
[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
您可以使用链接映射容器的参数(我们在这里使用名称,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
现在您可以访问IP了, Rabbitmq容器的端口,因为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屋!