Docker撰写和主机名

编程入门 行业动态 更新时间:2024-10-28 12:16:11
本文介绍了Docker撰写和主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个包含2个服务(容器)的名为 web和 db的撰写文件。

I have a compose file with 2 services (containers) named "web" and "db".

{ "version": "2", "services": { "web": { "image": "nodejs:latest", "ports": ["80"] }, "db": { "image": "mysql:latest", "ports": ["3306"] } } }

我能够通过使用 db作为数据库的ip从Web容器访问db容器。如下所示,在撰写文件中使用主机名有什么好处?

I am able to access db container from web container by using "db" as ip for my database. What advantage do i have by using "hostname" in the compose file as shown below?

{ "version": "2", "services": { "web": { "image": "nodejs:latest", "hostname": "web", "ports": ["80"] }, "db": { "image": "mysql:latest", "hostname": "db", "ports": ["3306"] } } }

推荐答案

在容器之间的专用网络中自动发现主机的功能已在 docker-compose语法的版本'2'。在您只能通过容器的链接别名引用容器之前。还有其他创建发现的解决方案-例如。添加代理或大使容器。

The feature of auto discovery of hosts in private network between container has been introduced in version '2' of docker-compose syntax. Before you were only able to reference containers by their link aliases. There were other solutions to create discovery - for ex. adding a proxy or 'ambassador' containers.

话虽如此,我会看到3个使用主机名的原因:

That being said I would see 3 reasons for using hostnames:

  • 发现
  • 一个服务(例如Web服务器)中可以有多个主机名
  • 如果要在不同环境之间保持一致的配置,则可以使用主机名来描述应用程序正在使用的其他端点,而不是依赖服务名

更多推荐

Docker撰写和主机名

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

发布评论

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

>www.elefans.com

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