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

查看:97
本文介绍了如何在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中链接容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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