{"id":2394,"date":"2023-01-09T10:35:02","date_gmt":"2023-01-09T15:35:02","guid":{"rendered":"https:\/\/shirishranjit.com\/blog1\/?page_id=2394"},"modified":"2023-01-09T10:35:27","modified_gmt":"2023-01-09T15:35:27","slug":"docker-how-to-get-hostname-of-the-container-docker-host-machine","status":"publish","type":"page","link":"https:\/\/shirishranjit.com\/blog1\/big-data\/docker-primer\/docker-how-to-get-hostname-of-the-container-docker-host-machine","title":{"rendered":"Docker &#8211; how to get hostname of the container\/Docker host machine"},"content":{"rendered":"\n<p>Docker container by default does not allow to peak into the host machine as it is ensures that the container does not have a specifically tied to the host machine. The principle is to have a docker that can run in any host machine. This allows to failover the docker to any machine.<\/p>\n\n\n\n<p>However, there are cases where a docker may need to know where it is running as a part of alerting. In such case, there are two ways to get the host machine name<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Map \/etc\/ to a volume in the docker. Then, \/etc\/hosts can be read by application<\/li>\n\n\n\n<li>As a part of docker run,  an environment name can be created by doing cat on the \/etc\/hostname for example:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -it -e \"HOSTNAME=$(cat \/etc\/hostname)\" &lt;image> &lt;cmd><\/code><\/pre>\n<div class=\"twttr_buttons\"><div class=\"twttr_twitter\">\n\t\t\t\t\t<a href=\"http:\/\/twitter.com\/share?text=Docker+-+how+to+get+hostname+of+the+container%2FDocker+host+machine\" class=\"twitter-share-button\" data-via=\"\" data-hashtags=\"\"  data-size=\"default\" data-url=\"https:\/\/shirishranjit.com\/blog1\/big-data\/docker-primer\/docker-how-to-get-hostname-of-the-container-docker-host-machine\"  data-related=\"\" target=\"_blank\">Tweet<\/a>\n\t\t\t\t<\/div><div class=\"twttr_followme\">\n\t\t\t\t\t\t<a href=\"https:\/\/twitter.com\/shiranjit\" class=\"twitter-follow-button\" data-size=\"default\"  data-show-screen-name=\"false\"  target=\"_blank\">Follow me<\/a>\n\t\t\t\t\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Docker container by default does not allow to peak into the host machine as it is ensures that the container does not have a specifically tied to the host machine. The principle is to have a docker that can run &hellip; <a href=\"https:\/\/shirishranjit.com\/blog1\/big-data\/docker-primer\/docker-how-to-get-hostname-of-the-container-docker-host-machine\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"parent":2199,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2394","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/2394"}],"collection":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/comments?post=2394"}],"version-history":[{"count":1,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/2394\/revisions"}],"predecessor-version":[{"id":2395,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/2394\/revisions\/2395"}],"up":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/2199"}],"wp:attachment":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/media?parent=2394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}