{"id":337480,"date":"2021-09-10T15:26:29","date_gmt":"2021-09-10T12:26:29","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-get-a-docker-containers-ip-address-from-the-host-cloudsavvy-it\/"},"modified":"2021-09-10T15:26:29","modified_gmt":"2021-09-10T12:26:29","slug":"how-to-get-a-docker-containers-ip-address-from-the-host-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-get-a-docker-containers-ip-address-from-the-host-cloudsavvy-it\/","title":{"rendered":"#How to Get a Docker Container\u2019s IP Address From the Host \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2e6ef93789f\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2e6ef93789f\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-a-docker-containers-ip-address-from-the-host-cloudsavvy-it\/#Consider_Using_a_User-Defined_Bridge\" >Consider Using a User-Defined Bridge<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-a-docker-containers-ip-address-from-the-host-cloudsavvy-it\/#Getting_The_IP_Address_From_Docker\" >Getting The IP Address From Docker<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-a-docker-containers-ip-address-from-the-host-cloudsavvy-it\/#Getting_Network_Config_From_The_Container\" >Getting Network Config From The Container<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Get a Docker Container\u2019s IP Address From the Host \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-503\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Docker containers aren\u2019t just about isolation\u2014they\u2019re often used to manage processes that still need to talk to each other directly. But, to communicate, you\u2019ll often need to know each container\u2019s IP address, which you can find with a few commands.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Consider_Using_a_User-Defined_Bridge\"><\/span>Consider Using a User-Defined Bridge<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker networking is a little complicated. Containers launched by default will be placed in the default \u201cbridge network,\u201d and are allowed to communicate with other containers directly, provided you have their private IP address. This can be turned off for true isolation, but it isn\u2019t by default.<\/p>\n<p>You can also use this address to communicate from the host OS if you don\u2019t want to bind a port. This is the primary use case of accessing a container directly through its IP address, but you should probably still just bind a port (you can keep it closed from the internet in your firewall).<\/p>\n<p>However, IP addresses are ephemeral, and can break easily when containers are stopped and started. For communication between containers, Docker provides a solution through <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/network\/bridge\/\">user-defined bridge networks,<\/a>\u00a0which you probably should be using if you have multiple containers talking to each other.<\/p>\n<p>Containers added to non-default networks will be able to access each other through their alias, which will resolve to the private IP automatically. You can create new networks, run containers in those networks, and connect existing containers to the network. You can then access the other containers using the alias as a hostname; for example, the NGINX container here can access the MongoDB instance with the connection string <code>mongodb:\/\/mongohost:27017<\/code>.<\/p>\n<pre>docker network create example&#13;\ndocker run --net example --name nginx -d nginx&#13;\ndocker network connect example --alias mongohost mongodb<\/pre>\n<p>There\u2019s a lot of upside to using bridges, and it\u2019s recommended over the legacy <code>--link<\/code>\u00a0option, which works on the default network. The primary issue is that containers in user-defined networks will be exposed to each other\u2019s ports, regardless of if they\u2019re published, but you can set up multiple networks, so this isn\u2019t usually an issue.<\/p>\n<p>The other downside is that because user-defined networks provide better isolation, they also disallow you to access containers across networks with their private IP address. All containers in the default network can talk to each other, but once it\u2019s removed and placed in a user-defined network, that ability is disabled. However, you can also just launch the container in both the default and user-defined networks, so it\u2019s not a problem if you choose to make the container visible to others.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Getting_The_IP_Address_From_Docker\"><\/span>Getting The IP Address From Docker<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you just want the IP address though, it\u2019s pretty simple to get from the host OS. First, you\u2019ll need to find the ID or name of the container you want to get the information for, which you can do with:<\/p>\n<pre>docker ps<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14125\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/c95f5758.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"561\" height=\"140\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Then, run <code>docker inspect<\/code>, which returns a huge JSON file with all the information about the container. We\u2019re only interested in the IP address though, so you can pass it a formatting option with <code>-f<\/code>\u00a0to narrow it down to just the address.<\/p>\n<pre>docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' name_or_id<\/pre>\n<p>This command works well, but it\u2019s also only returning info for one container which you will need the ID of. If you want a more concrete solution, you can use <code>docker network inspect<\/code>\u00a0which prints info about all containers in the given network, optionally formatting as a JSON lookup table:<\/p>\n<pre>docker network inspect bridge -f '{{json .Containers}}'<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14126\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/c45bd8ab.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"746\" height=\"636\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Getting_Network_Config_From_The_Container\"><\/span>Getting Network Config From The Container<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker containers are really just an isolation mechanism, and nothing is preventing you from just entering the container and running regular Linux commands like <code>ifconfig<\/code>\u00a0and getting the IP address that way.<\/p>\n<p>To do that, you\u2019ll need to grab the container name or ID with <code>docker ps<\/code>, then run <code>exec -it<\/code>, in this case, printing out all IP information:<\/p>\n<pre>docker exec -it b94ef3169cd4 ip a<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14127\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/83e6e8f6.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"810\" height=\"258\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14119\/how-to-get-a-docker-containers-ip-address-from-the-host\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Get a Docker Container\u2019s IP Address From the Host \u2013 CloudSavvy IT&#8221; Docker containers aren\u2019t just about isolation\u2014they\u2019re often used to manage processes that still need to talk to each other directly. But, to communicate, you\u2019ll often need to know each container\u2019s IP address, which you can find with a few commands. Consider&#8230;<\/p>\n","protected":false},"author":1,"featured_media":337481,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-337480","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/337480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=337480"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/337480\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/337481"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=337480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=337480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=337480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}