如何将主机名添加到同一 docker 网络上的容器? [英] How can I add hostnames to a container on the same docker network?

查看:30
本文介绍了如何将主机名添加到同一 docker 网络上的容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含两个容器的 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

在此示例中,默认网络上的其他容器可以使用 dbdatabase 访问 db 服务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= 选项.

You can also add aliases to running containers using the docker network connect command with the --alias= option.

这篇关于如何将主机名添加到同一 docker 网络上的容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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