gpt4 book ai didi

Docker 链接容器、Docker 网络、Compose 网络 - 我们现在应该如何 'link' 容器

转载 作者:IT老高 更新时间:2023-10-28 21:39:13 32 4
gpt4 key购买 nike

我有一个现有的应用程序,其中包含在同一主机上运行的 4 个 docker 容器。它们已使用 link 命令链接在一起。

然而,在 docker 升级后,link 行为已被弃用,并且似乎有所改变。我们现在遇到了容器失去彼此链接的问题。

所以,docker 说要在 linked 容器上使用新的 Network 功能。但我看不出这是如何工作的。

如果 2 个容器在同一个网络中,相同的 ENV 变量是否会自动暴露在容器上,就好像它们是链接的一样?

或者是否使用正确的容器名称/IP 地址更新了主机文件?即使在 docker restart 之后?

我在文档中看不到容器如何在其网络中找到另一个容器的位置?

另外,compose 看起来有一个用于链接容器的简单设置,并且可能会自动执行其中的一些操作 - 组合是定义多容器应用程序的方式吗?还是在生产中运行它还为时过早?

compose 是否也支持多主机配置?

在未来的某个时候,我们可能需要将其中一个容器移动到不同的主机......

最佳答案

If 2 containers are in the same network, are the same ENV vars automatically exposed on the containers as if they were linked?

不,您现在必须使用容器名称作为它们的主机名。新的网络功能不知道将使用哪些端口。将其视为插入同一网络集线器的 2 台计算机。两者都可以通过主机名来寻址另一个。

is the hosts file updated with the correct container name / ip addresses ? Even after a docker restart ?

是的,作为网络一部分的所有容器的 /etc/hosts 文件将由 docker 引擎实时更新。

I can't see in the docs how a container can find the location of another in its network?

使用容器名称。见 Connect containers Work with network commands 文档的部分:一旦连接,容器就可以使用另一个容器的 IP 地址或名称进行通信。

Also, compose looks to have a simple set up for linking containers, and may automate some of this - would compose be the way to go for defining multi container apps? Or is it too soon to run it in production?

Compose 通过提供 --x-networking 选项来支持新的网络功能作为测试版。您不应该在生产中使用它(当前 Compose 版本是 1.5)。

此外,当前的实现有点不方便,因为我们必须使用由 项目名称 + _ + 容器名称组成的完整容器名称 + _1documentation说下一个版本(当前版本是 1.5)会改进这一点,这样我们就不必担心 项目名称 来寻址容器。

Does compose support multiple host configuration as well?

是的,与 overlay network documentation 中详述的 Swarm 结合使用

关于Docker 链接容器、Docker 网络、Compose 网络 - 我们现在应该如何 'link' 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34727352/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com