如何在Docker中链接容器? [英] How to link containers in docker?

查看:63
本文介绍了如何在Docker中链接容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我输入 docker ps 时的结果:

Here's the result when I type docker ps :

我有3个Docker容器:webapps,redis和Rabbitmq.我想将容器Webapp链接到容器Redis和Rabbitmq容器.在非docker应用程序中,mywebapps可以将消息发送到Rabbitmq并写入/读取Redis.

I have 3 docker containers: webapps, redis and rabbitmq. I want to link container webapps to container redis and rabbitmq container. In non docker apps, mywebapps can send message to rabbitmq and write/read redis.

我尝试使用这样的命令

docker run --name rabbitmq -p 8080:80 --link webapps:nimmis/apache-php7 -d rabbitmq

但它不起作用.

这是我在webapps上的config.php,我正尝试通过Rabbitmq发送消息:

Here is my config.php on webapps where I am trying to send messages via rabbitmq:

define('HOST', 'localhost');
define('PORT', 5672);

我试图用主机名更改localhost

I tried to change localhost with hostname

define('HOST', 'rabbitmq');
define('PORT', 5672);

错误消息说连接被拒绝.

似乎在我的三个容器中需要在同一网络名称空间中进行配置.

It seems that in my three containers needs to be configured in the same network namespace.

推荐答案

链接是一项旧功能.请使用用户定义的网络":

Linking is a legacy feature. Please use "user defined networks":

sudo docker network create mynetwork

然后使用此网络重新运行容器:

Then rerun your containers using this network:

sudo docker run --name rabbitmq -p 8080:80 -d --network mynetwork rabbitmq

对要彼此连接的其他容器执行相同的操作.

Do the same for other containers that you want connected with each other.

使用用户定义的网络",您可以使用内部名称解析"(有点像访问网站时的域名解析).您可以使用要引用的容器名称,以便解析容器的IP地址,只要它们在同一用户定义的网络"上运行即可.这样,您就可以使用同一网络中其他容器中的 rabbitmq 容器的名称解析其IP地址.

Using "user defined networks", you have an "internal name resolution" at your disposal (somewhat like domain name resolution when visiting websites). You can use the names of the container that you want to refer to, in order to resolve the IP addresses of containers, as long as they are running on the same "user defined network". With this, you can resolve the IP address of the rabbitmq container with its name, within other containers, on the same network.

所有容器将具有网络连接性.不需要旧版链接".

All containters on the same "user defined network" will have network connectivity. There is no need for "legacy linking".

这篇关于如何在Docker中链接容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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