如何将主机名添加到同一docker网络上的容器? [英] How can I add hostnames to a container on the same docker network?
问题描述
假设我有一个包含两个容器的docker compose文件。两者都在其/ etc / hosts文件中相互引用。容器A具有容器B的参考,反之亦然。所有这些都是自动发生的。现在,我想在A的主机文件中向B添加一个或多个主机名。我该怎么做呢?我可以在Docker Compose中实现这一目标吗?
Suppose I have a docker compose file with two containers. Both reference each other in their /etc/hosts file. Container A has a reference for container B and vice versa. And all of this happens automatically. Now I want to add one or more hostnames to B in A's hosts file. How can I go about doing this? Is there a special way I can achieve this in Docker Compose?
示例:
172.0.10.166 service- b my-custom-hostname
172.0.10.166 service-b my-custom-hostname
推荐答案
是。在撰写文件中,您可以指定网络别名。
Yes. In your compose file, you can specify network aliases.
services:
db:
networks:
default:
aliases:
- database
- postgres
在此示例中, db 默认网络上的其他容器可以使用
。 db
,数据库
或<$来访问code>服务c $ c> postgres
In this example, the db
service could be reached by other containers on the default network using db
, database
, or postgres
.
您还可以使用 docker network connect
命令和-alias =
选项。
这篇关于如何将主机名添加到同一docker网络上的容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!