有没有一种方法可以将主机名添加到现有的Docker容器中? [英] Is there a way to add a hostname to an EXISTING docker container?

查看:73
本文介绍了有没有一种方法可以将主机名添加到现有的Docker容器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些容器通过其IP从网络docker进行通信。

I have some containers that communicate via their IP from the network docker.

我可以使用选项 -h -主机名,但要设置现有容器的主机名。

I can use the option -h or --hostname when running a new container but I want to set the hostname for existing container.

推荐答案

一种方法是创建网络并在该网络中添加其他容器。
在网络中添加容器时,可以使用 docker network -alias 选项。像这样:

One way is to create network and add different container in this network. When adding container in the network, you can use the --alias option of docker network. Like this:


  • 创建网络:

  • Create a network:

docker network create <my-network-name> 


  • 在网络中添加容器:

  • Add containers in the network:

    docker network connect --alias <hostname-container-1> <my-network-name> <container-1>
    docker network connect --alias <hostname-container-2> <my-network-name> <container-2>
    docker network connect --alias <hostname-container-3> <my-network-name> <container-3>
    


  • 享受。

  • Enjoy.

    因此,每个容器都可以通过别名看到其他容器(该别名用作主机名)。

    So each container can see other container by the alias (the alias is used as hostname).

    这篇关于有没有一种方法可以将主机名添加到现有的Docker容器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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