如何在Docker中链接容器?

编程入门 行业动态 更新时间:2024-10-20 18:59:18
本文介绍了如何在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中链接容器?

本文发布于:2023-10-07 15:12:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469732.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   链接   如何在   Docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!