{"id":339122,"date":"2021-09-14T15:12:25","date_gmt":"2021-09-14T12:12:25","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-connect-to-localhost-within-a-docker-container-cloudsavvy-it\/"},"modified":"2021-09-14T15:12:25","modified_gmt":"2021-09-14T12:12:25","slug":"how-to-connect-to-localhost-within-a-docker-container-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-connect-to-localhost-within-a-docker-container-cloudsavvy-it\/","title":{"rendered":"#How to Connect to Localhost Within a Docker Container \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_87 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-6a9c58ae41c79\" 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-6a9c58ae41c79\" 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-connect-to-localhost-within-a-docker-container-cloudsavvy-it\/#The_Easy_Option\" >The Easy Option<\/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-connect-to-localhost-within-a-docker-container-cloudsavvy-it\/#Connecting_to_the_Host_Network\" >Connecting to the Host Network<\/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-connect-to-localhost-within-a-docker-container-cloudsavvy-it\/#Accessing_the_Host_With_the_Default_Bridge_Mode\" >Accessing the Host With the Default Bridge Mode<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-connect-to-localhost-within-a-docker-container-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Connect to Localhost Within a Docker Container \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-10864\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg?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\/2021\/04\/075c8694.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Illustration showing the Docker logo\" width=\"1600\" height=\"900\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>When working with Docker, you usually containerize the services that form your stack and use inter-container networking to communicate between them. Sometimes you might need a container to talk to a service on your host that hasn\u2019t been containerized. Here\u2019s how to access <code>localhost<\/code> or <code>127.0.0.1<\/code> from within a Docker container.<\/p>\n<h2 id=\"the-easy-option\"><span class=\"ez-toc-section\" id=\"The_Easy_Option\"><\/span>The Easy Option<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker Desktop 18.03+ for Windows and Mac supports <code>host.docker.internal<\/code> as a functioning alias for <code>localhost<\/code>. Use this string inside your containers to access your host machine.<\/p>\n<ul>\n<li><strong><code>localhost<\/code> and <code>127.0.0.1<\/code><\/strong> \u2013 These resolve to the <em>container<\/em>.<\/li>\n<li><strong><code>host.docker.internal<\/code><\/strong> \u2013 This resolves to the outside host.<\/li>\n<\/ul>\n<p>If you\u2019re running a MySQL server on your host, Docker containers could access it by connecting to <code>host.docker.internal:3306<\/code>. This is the simplest technique when you\u2019re working on a Windows or Mac machine.<\/p>\n<p>Docker Engine users on Linux can enable <code>host.docker.internal<\/code> too via the <code>--add-host<\/code> flag for <code>docker run<\/code>. Start your containers with this flag to expose the host string:<\/p>\n<pre>docker run -d --add-host host.docker.internal:host-gateway my-container:latest<\/pre>\n<p>The <code>--add-host<\/code> flag adds an entry to the container\u2019s <code>\/etc\/hosts<\/code> file. The value shown above maps <code>host.docker.internal<\/code> to the container\u2019s host gateway, which matches the real <code>localhost<\/code> value. You could replace <code>host.docker.internal<\/code> with your own string if you prefer.<\/p>\n<h2 id=\"connecting-to-the-host-network\"><span class=\"ez-toc-section\" id=\"Connecting_to_the_Host_Network\"><\/span>Connecting to the Host Network<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker provides a <code>host<\/code> network which lets containers share your host\u2019s networking stack. This <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>roach means <code>localhost<\/code> inside a container resolves to the physical host, instead of the container itself.<\/p>\n<p>Containers are launched with the host network by adding the <code>--network=host<\/code> flag:<\/p>\n<pre>docker run -d --network=host my-container:latest<\/pre>\n<p>Now your container can reference <code>localhost<\/code> or <code>127.0.0.1<\/code> directly.<\/p>\n<p>If you\u2019re using Docker Compose, modify your container\u2019s service definition to include the <code>network_mode<\/code> field:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><span class=\"co4\">services<\/span>:<span class=\"co4\">\n  my-service<\/span>:<span class=\"co3\">\n    network_mode<\/span><span class=\"sy2\">: <\/span>host<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>There are some caveats to this approach. It\u2019s important to consider all the implications before you use it. Containers ordinarily get their own private network that\u2019s separate to the host\u2019s stack. When you specify <code>--network=host<\/code>, the container defaults to inheriting shared networking settings from your host.<\/p>\n<p>Any ports exposed by the container will be exposed <em>on the host<\/em>, even if they\u2019re not explicitly declared with the <code>-p<\/code> flag. The container\u2019s default hostname will match the host\u2019s, although this can be changed with the <code>--hostname<\/code> flag.<\/p>\n<p>The host network can be a security concern which breaks the isolation model of Docker containers. It can still be useful in scenarios where you\u2019re confident that running containers won\u2019t conflict with each other or cause problems in your host environment. The host networking mode is also <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/run\/#network-settings\">quicker than<\/a> the default bridge mode as there\u2019s no virtualization layer for traffic to pass through.<\/p>\n<h2 id=\"accessing-the-host-with-the-default-bridge-mode\"><span class=\"ez-toc-section\" id=\"Accessing_the_Host_With_the_Default_Bridge_Mode\"><\/span>Accessing the Host With the Default Bridge Mode<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Your host can still be accessed from containers in the default <code>bridge<\/code> networking mode. You just need to reference it by its Docker network IP, instead of <code>localhost<\/code> or <code>127.0.0.1<\/code>.<\/p>\n<p>Most Docker Engine installations will represent the host as <code>172.17.0.1<\/code> on the default <code>docker0<\/code> bridge network. You can check your own IP by running this command on your host:<\/p>\n<pre>ip addr show docker0<\/pre>\n<p>Your host\u2019s Docker IP will be shown on the <code>inet<\/code> line. Connect to this IP address from within your containers to successfully access the services running on your host.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-14110\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/bb5d62a9.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"811\" height=\"149\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>One pitfall of this approach is you might not be able to connect to services which bind directly to <code>localhost<\/code>. You\u2019ll need to make sure your services are listening for connections on your Docker bridge IP, as well as <code>localhost<\/code> and <code>127.0.0.1<\/code>. Otherwise you\u2019ll see <code>connection refused<\/code> or similar errors within your container.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You\u2019ve got several options when you need to reach outside a Docker container to your machine\u2019s <code>localhost<\/code>. If you\u2019re on Windows or Mac, it\u2019s best to use the built-in <code>host.docker.internal<\/code> alias. Linux users can setup something similar with the <code>--add-host<\/code> flag when starting a container.<\/p>\n<p>Host networking mode is a universal alternative which lets containers share your host\u2019s networking stack. You can reference <code>localhost<\/code> directly but need to stay aware of the risks and limitations. It\u2019s not a suitable option when strong networking isolation is required.<\/p>\n<p>Sticking with bridge mode can be the best option for workloads which support it. Bind your host\u2019s services to its Docker IP, then use that address to connect from within your container. This lets you use Docker\u2019s per-container virtualized networking while providing a route to your host when it\u2019s required.\n<\/p><\/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\/14114\/how-to-connect-to-localhost-within-a-docker-container\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Connect to Localhost Within a Docker Container \u2013 CloudSavvy IT&#8221; When working with Docker, you usually containerize the services that form your stack and use inter-container networking to communicate between them. Sometimes you might need a container to talk to a service on your host that hasn\u2019t been containerized. Here\u2019s how to access&#8230;<\/p>\n","protected":false},"author":1,"featured_media":339123,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-339122","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\/339122","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=339122"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/339122\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/339123"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=339122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=339122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=339122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}