简体   繁体   English

自动在Docker容器中提取主机名的方法

[英]Automatic way to pick up the hostname inside docker container

We're running NodeJS application inside docker container hosted on Amazon EC2 instance. 我们正在Amazon EC2实例上托管的docker容器内运行NodeJS应用程序。 To

To enable Monitoring for Node.js app with Datadog we are using datadog-metrics library and integrate it with our application. 为了使用Datadog启用对Node.js的监视应用程序,我们使用了datadog-metrics库并将其与我们的应用程序集成。 We basically require to save the below Javascript code into a file called example_app.js 我们基本上需要将以下Javascript代码保存到名为example_app.js的文件中

 var metrics = require('datadog-metrics'); metrics.init({ **host: 'myhost', prefix: 'myapp.'** }); function collectMemoryStats() { var memUsage = process.memoryUsage(); metrics.gauge('memory.rss', memUsage.rss); metrics.gauge('memory.heapTotal', memUsage.heapTotal); metrics.gauge('memory.heapUsed', memUsage.heapUsed); metrics.increment('memory.statsReported'); } setInterval(collectMemoryStats, 5000); 

Although, we are able to successfully publish metrics to datadog but we're wondering if this can be automated. 虽然,我们能够成功将指标发布到datadog,但我们想知道这是否可以自动化。 We want build this into our docker image, hence require an automatic way to pick up the hostname, at the very least be able to use the docker hosts name if possible..Because till now we're manually specifying "myhost" and "myapp" values manually. 我们要将其构建到我们的docker映像中,因此需要一种自动的方式来选择主机名,如果可能的话,至少要能够使用docker主机名。.因为到目前为止,我们手动指定了“ myhost”和“ myapp” ”值手动设置。 Any better way to fetch the AWS instance hostname value into %myhost? 还有什么更好的方法可以将AWS实例主机名值提取到%myhost中?

Why not try? 为什么不尝试?

 var os = require(“os”);
 var hostname = os.hostname();

It will return the docker container's hostname. 它将返回Docker容器的主机名。 If you haven't set a hostname explicitly, using something like docker run -h hostname image command then it will return the docker host's hostname. 如果您尚未使用docker run -h hostname image command明确设置主机名,它将返回Docker主机的主机名。

Alternatively, you could do this using a deployment tool like puppet, ansible, etc. and template the file when you deploy the container. 另外,您可以使用诸如puppet,ansible等部署工具来执行此操作,并在部署容器时对文件进行模板化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM