如何在没有env vars的情况下从该主机上的docker容器内部获取docker主机的主机名 [英] How to get the hostname of the docker host from inside a docker container on that host without env vars

查看:104
本文介绍了如何在没有env vars的情况下从该主机上的docker容器内部获取docker主机的主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了使用环境变量外,还有什么方法可以从在该主机上运行的容器内部获取Docker主机的主机名? 我知道我可以在创建容器时将主机名作为环境变量传递给容器。我想知道如何在运行时查找它。

What are the ways get the docker host's hostname from inside a container running on that host besides using environment variables? I know I can pass the hostname as an environment variable to the container at container creation time. I'm wondering how I can look it up at run time.

foo.example.com (docker host)
  bar (docker container)

有没有一种方法可以使容器 bar 在docker host foo.example.com 以获得 foo.example.com?

Is there a way for container bar running in docker host foo.example.com to get "foo.example.com"?

编辑以添加用例:

容器将为服务发现创建SRV记录,格式为

The container will create an SRV record for service discovery of the form

_service._proto.name. TTL class SRV priority weight port target.
-----------------------------------------------------------------
_bar._http.example.com 60 IN SRV 5000 5000 20003 foo.example.com.

其中20003是docker主机上的动态分配端口,用于服务监听bar中的某个固定端口(docker处理从主机端口到容器端口的映射。)

where 20003 is a dynamically allocated port on the docker host for a service listening on some fixed port in bar (docker handles the mapping from host port to container port).

我的容器将运行运行状况检查,以确保它成功创建了SRV记录,因为会有很多其他docker主机上的其他bar容器也创建了自己的SRV记录。

My container will run a health check to make sure it has successfully created that SRV record as there will be many other bar containers on other docker hosts that also create their own SRV records.

_service._proto.name. TTL class SRV priority weight port target.
-----------------------------------------------------------------
_bar._http.example.com 60 IN SRV 5000 5000 20003 foo.example.com. <--
_bar._http.example.com 60 IN SRV 5000 5000 20003 foo2.example.com.
_bar._http.example.com 60 IN SRV 5000 5000 20003 foo3.example.com.

健康检查将遍历SRV记录以查找上面的第一个记录,因此需要知道其主机名。

The health check will loop through the SRV records looking for the first one above and thus needs to know its hostname.

一旁

我正在使用 Helios ,发现它添加了为我提供的环境变量,从中可以获得主机名。但是我只是很好奇,以防万一我使用的是不带Helios的docker。

I'm using Helios and just found out it adds an env var for me from which I can get the hostname. But I was just curious in case I was using docker without Helios.

推荐答案

您可以将主机名作为环境变量传递。您也可以挂载/ etc,以便可以使用/ etc / hostname。但是我同意Vitaly,这不是容器IMO的预期用例。

You can pass in the hostname as an environment variable. You could also mount /etc so you can cat /etc/hostname. But I agree with Vitaly, this isn't the intended use case for containers IMO.

这篇关于如何在没有env vars的情况下从该主机上的docker容器内部获取docker主机的主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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