如何在Docker中链接容器?

编程入门 行业动态 更新时间:2024-10-21 03:21:33
本文介绍了如何在Docker中链接容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我键入 docker ps 时,结果如下:

Here's the result when I type docker ps :

时的图像,我有3个容器,webapps,redis和Rabbitmq,我对如何链接容器感到困惑webapps到容器redis和Rabbitmq容器? ,在非docker应用中,mywebapps可以将消息发送到Rabbitmq并写入/读取reddis。我仍然不知道如何链接3个这些容器,i尝试使用这样的命令

in docker, i have 3 containers , webapps , redis and rabbitmq, i am confusing how to link container webapps to container redis and rabbitmq container ? , in non docker apps, mywebapps can send message to rabbitmq and write/read reddis. i stil don't have any idea how to link 3 those container, i tried using command like this

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

但仍然不起作用

当我尝试通过Rabbitmq发送消息时,这是我在webapp上的config.php

here is my config.php on webapps when i try 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);

错误消息是连接被拒绝... 在我的三个容器中似乎需要在同一ip中配置/映射/绑定网络?

error messages is connection refused... it seems that in my three container needs to configure/mapping/bind network in same ip ?

推荐答案

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

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

使用用户定义的网络,您可以使用内部名称解析(有点像域名解析,访问网站)。您可以使用要引用的容器名称,以便解析容器的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:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469731.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   链接   如何在   Docker

发布评论

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

>www.elefans.com

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