How to access host port from docker container - Stack Overflow (https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a) (Stack Overflow) (https://stackoverflow.com/opensearch.xml) (https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container) (site logo) Join Stack Overflow By clicking “Sign up”, you agree to our (https://stackoverflow.com/legal/terms-of-service/public) terms of service and acknowledge you have read our (https://stackoverflow.com/legal/privacy-policy) privacy policy . Sign up with Google Sign up with GitHub OR Email Password (8+ characters (at least 1 letter & 1 number)) Sign up Already have an account? (https://stackoverflow.com/users/login) Log in Skip to main content (https://stackoverflow.com/) Stack Overflow (https://stackoverflow.co/) About Products (https://stackoverflow.co/teams/ai/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav-bar&utm_content=overflowai) OverflowAI (Search…) (Click to show search) (https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f31324981%2fhow-to-access-host-port-from-docker-container%2f43541732) Log in (https://stackoverflow.com/users/signup?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f31324981%2fhow-to-access-host-port-from-docker-container%2f43541732) Sign up Let's set up your homepage Select a few topics you're interested in: python javascript c# reactjs java android html flutter c++ node.js typescript css r php angular next.js spring-boot machine-learning sql excel ios azure docker Or search from our full list: (Search) Next You’ll be prompted to create an account to view your personalized homepage. (https://stackoverflow.com/) Home (https://stackoverflow.com/questions) Questions (https://stackoverflow.com/tags) Tags (https://stackoverflow.com/beta/discussions) Discussions Labs (https://chat.stackoverflow.com/?tab=all&sort=active) Chat (https://stackoverflow.com/users) Users (https://stackoverflow.com/jobs?source=so-left-nav) Jobs (https://stackoverflow.com/jobs/companies?so_medium=stackoverflow&so_source=SiteNav) Companies Collectives Communities for your favorite technologies. (https://stackoverflow.com/collectives-all) Explore all Collectives Teams Ask questions, find answers and collaborate at work with Stack Overflow for Teams. (https://stackoverflowteams.com/teams/create/free/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) Try Teams for free (https://stackoverflow.co/teams/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) Explore Teams (Illustration of upvote icon after it is clicked) Hang on, you can't upvote just yet. You'll need to complete a few actions and gain 15 reputation points before being able to upvote. Upvoting indicates when questions and answers are useful. (https://stackoverflow.com/help/whats-reputation) What's reputation and how do I get it? Instead, you can save this post to reference later. Save this post for later Not now (Illustration of upvote icon after it is clicked) Hang on, you can't downvote just yet. You'll need to complete a few actions and gain 125 reputation points before being able to downvote. Casting downvotes indicate issues with quality, effort, or accuracay of a post. (https://stackoverflow.com/help/whats-reputation) What's reputation and how do I get it? Instead, you can try suggesting an edit to improve the post. Suggest edit No thanks (Illustration of upvote icon after it is clicked) Hang on, you can't upvote just yet. You'll need to complete a few actions and gain 15 reputation points before being able to upvote. Upvoting indicates when questions and answers are useful. (https://stackoverflow.com/help/whats-reputation) What's reputation and how do I get it? To start gaining reputation, try answering a related question. docker docker-container Find related question Not now (https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a) (https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container) How to access host port from docker container [duplicate] (https://stackoverflow.com/questions/ask) Ask Question (2015-07-09 18:01:28Z) Asked 9 years, 9 months ago Modified (https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container/43541732?lastactivity) (2025-02-14 13:55:16Z) 2 months ago (Viewed 968,141 times) Viewed 968k times This question shows research effort; it is useful and clear 799 (This question does not show any research effort; it is unclear or not useful) Save this question. (https://stackoverflow.com/posts/31324981/timeline) Show activity on this post. This question already has answers here : (https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) From inside of a Docker container, how do I connect to the localhost of the machine? [closed] (45 answers) Closed (2022-03-14 09:52:18Z) 3 years ago . I have a docker container running jenkins. As part of the build process, I need to access a web server that is run locally on the host machine. Is there a way the host web server (which can be configured to run on a port) can be exposed to the jenkins container? I'm running docker natively on a Linux machine. UPDATE: In addition to @larsks answer below, to get the IP address of the Host IP from the host machine, I do the following: ip addr show docker0 | grep -Po 'inet \K[\d.]+' (https://stackoverflow.com/questions/tagged/docker) (show questions tagged 'docker') docker (https://stackoverflow.com/questions/tagged/docker-container) (show questions tagged 'docker-container') docker-container (https://stackoverflow.com/q/31324981) (Short permalink to this question) Share (https://stackoverflow.com/posts/31324981/edit) Improve this question Follow Follow this question to receive notifications (https://stackoverflow.com/posts/31324981/revisions) (show all edits to this post) edited (2022-03-14 09:52:04Z) Mar 14, 2022 at 9:52 (https://stackoverflow.com/users/4685471/desertnaut) (desertnaut's user avatar) (https://stackoverflow.com/users/4685471/desertnaut) desertnaut (reputation score 60,494) 60.5k (32 gold badges) 32 32 gold badges (155 silver badges) 155 155 silver badges (182 bronze badges) 182 182 bronze badges asked (2015-07-09 18:01:28Z) Jul 9, 2015 at 18:01 (https://stackoverflow.com/users/1368032/tri-nguyen) (Tri Nguyen's user avatar) (https://stackoverflow.com/users/1368032/tri-nguyen) Tri Nguyen Tri Nguyen (reputation score 11,178) 11.2k (9 gold badges) 9 9 gold badges (48 silver badges) 48 48 silver badges (78 bronze badges) 78 78 bronze badges 8 Using a comment since this is a terrible answer, but I believe you can generally access it on 172.17.1.78 -- unless this is a boot2docker setup. – (https://stackoverflow.com/users/427204/cashisclay) (2,330 reputation) CashIsClay Commented (2015-07-09 18:16:11Z, License: CC BY-SA 3.0) Jul 9, 2015 at 18:16 (number of 'useful comment' votes received) 2 @CashIsClay I tried that, and got this error still curl: (7) Failed to connect to 172.17.1.78 port 7000: No route to host – (https://stackoverflow.com/users/1368032/tri-nguyen) (11,178 reputation) Tri Nguyen Commented (2015-07-09 18:41:30Z, License: CC BY-SA 3.0) Jul 9, 2015 at 18:41 (this comment was edited 1 time) You didn't specify; are you running boot2docker, or are you running Docker natively on Linux? – (https://stackoverflow.com/users/147356/larsks) (313,955 reputation) larsks Commented (2015-07-09 20:34:22Z, License: CC BY-SA 3.0) Jul 9, 2015 at 20:34 (number of 'useful comment' votes received) 5 Now for Docker Desktop in Win 10(with Linux container) host.docker.internal also works, I just have to prepend http:// before it for my Java config files for the URL. Hope it helps someone. – (https://stackoverflow.com/users/4537090/westerngun) (12,885 reputation) WesternGun Commented (2020-08-10 11:24:04Z, License: CC BY-SA 4.0) Aug 10, 2020 at 11:24 (this comment was edited 1 time) (number of 'useful comment' votes received) 3 I think if a question has 17 answers and 650 upvotes, it probably shouldn't have been closed. – (https://stackoverflow.com/users/120603/trojanname) (5,405 reputation) TrojanName Commented (2023-02-01 11:51:26Z, License: CC BY-SA 4.0) Feb 1, 2023 at 11:51 (Use comments to ask for more information or suggest improvements. Avoid answering questions in comments.) | (Expand to show all comments on this post) Show 3 more comments 17 Answers 17 Sorted by: (https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container?answertab=scoredesc#tab-top) Reset to default (scoredesc) Highest score (default) (trending) Trending (recent votes count more) (modifieddesc) Date modified (newest first) (createdasc) Date created (oldest first) This answer is useful 1037 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/43541732/timeline) Show activity on this post. For all platforms Docker v 20.10 and above (since December 14th 2020) Use your internal IP address or connect to the special DNS name host.docker.internal which will resolve to the internal IP address used by the host. This is for development purpose and does not work in a production environment outside of Docker Desktop. Linux caveats To enable this in Docker on Linux, add --add-host=host.docker.internal:host-gateway to your docker command to enable the feature. To enable this in Docker Compose on Linux, add the following lines to the container definition: extra_hosts: - "host.docker.internal:host-gateway" According to some users the special DNS name only works within the Docker's default bridge network, not within custom networks. For older macOS and Windows versions of Docker Docker v 18.03 and above (since March 21st 2018) Use your internal IP address or connect to the special DNS name host.docker.internal which will resolve to the internal IP address used by the host. Linux support pending (https://github.com/docker/for-linux/issues/264) https://github.com/docker/for-linux/issues/264 For older macOS versions of Docker Docker for Mac v 17.12 to v 18.02 Same as above but use docker.for.mac.host.internal instead. Docker for Mac v 17.06 to v 17.11 Same as above but use docker.for.mac.localhost instead. Docker for Mac 17.05 and below To access host machine from the docker container you must attach an IP alias to your network interface. You can bind whichever IP you want, just make sure you're not using it to anything else. sudo ifconfig lo0 alias 123.123.123.123/24 Then make sure that you server is listening to the IP mentioned above or 0.0.0.0 . If it's listening on localhost 127.0.0.1 it will not accept the connection. Then just point your docker container to this IP and you can access the host machine! To test you can run something like curl -X GET 123.123.123.123:3000 inside the container. The alias will reset on every reboot so create a start-up script if necessary. Solution and more documentation here: (https://docs.docker.com/desktop/networking/#use-cases-and-workarounds-for-all-platforms) https://docs.docker.com/desktop/networking/#use-cases-and-workarounds-for-all-platforms (https://stackoverflow.com/a/43541732) (Short permalink to this answer) Share (https://stackoverflow.com/posts/43541732/edit) Improve this answer Follow Follow this answer to receive notifications (https://stackoverflow.com/posts/43541732/revisions) (show all edits to this post) edited (2024-02-26 12:01:51Z) Feb 26, 2024 at 12:01 (https://stackoverflow.com/users/7508077/esmaeele) (EsmaeelE's user avatar) (https://stackoverflow.com/users/7508077/esmaeele) EsmaeelE (reputation score) 2,736 (7 gold badges) 7 7 gold badges (27 silver badges) 27 27 silver badges (35 bronze badges) 35 35 bronze badges answered (2017-04-21 11:36:02Z) Apr 21, 2017 at 11:36 (https://stackoverflow.com/users/2713729/janne-annala) (Janne Annala's user avatar) (https://stackoverflow.com/users/2713729/janne-annala) Janne Annala Janne Annala (reputation score 28,876) 28.9k (9 gold badges) 9 9 gold badges (34 silver badges) 34 34 silver badges (43 bronze badges) 43 43 bronze badges 15 (number of 'useful comment' votes received) 1 I've successfully tested this. No need to turn off the firewall – (https://stackoverflow.com/users/1181094/alvaro-g) (533 reputation) alvaro g Commented (2017-05-22 13:49:35Z, License: CC BY-SA 3.0) May 22, 2017 at 13:49 (number of 'useful comment' votes received) 19 changed to host.docker.internal (https://docs.docker.com/docker-for-mac/networking/#i-cannot-ping-my-containers) docs.docker.com/docker-for-mac/networking/… – (https://stackoverflow.com/users/2485910/snowball) (1,512 reputation) Snowball Commented (2018-03-27 12:17:50Z, License: CC BY-SA 3.0) Mar 27, 2018 at 12:17 (number of 'useful comment' votes received) 1 Is host.docker.internal supposed to work also on Ubuntu? After having run docker run -it ubuntu:trusty bash the command root@40a5887b060d:/# ping host.docker.internal gives me ping: unknown host host.docker.internal – (https://stackoverflow.com/users/1310566/simon-forsberg) (13,380 reputation) Simon Forsberg Commented (2018-04-14 16:08:20Z, License: CC BY-SA 3.0) Apr 14, 2018 at 16:08 (number of 'useful comment' votes received) 2 Recommend use host.docker.internal in the docker container and config 127.0.0.1 host.docker.internal in hosts file. – (https://stackoverflow.com/users/4281309/junlin) (2,055 reputation) junlin Commented (2018-06-19 11:14:26Z, License: CC BY-SA 4.0) Jun 19, 2018 at 11:14 (this comment was edited 2 times) (number of 'useful comment' votes received) 1 host.docker.internal work perfect for me! in my case the WSL change the IP on every reboot :/ – (https://stackoverflow.com/users/8009061/albert-hidalgo) (113 reputation) Albert Hidalgo Commented (2021-06-24 15:57:27Z, License: CC BY-SA 4.0) Jun 24, 2021 at 15:57 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) | (Expand to show all comments on this post) Show 10 more comments This answer is useful 403 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/31328031/timeline) Show activity on this post. When running Docker natively on Linux, you can access host services using the IP address of the docker0 interface. From inside the container, this will be your default route. For example, on my system: $ ip addr show docker0 7: docker0: mtu 1500 qdisc noqueue state DOWN group default link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::f4d2:49ff:fedd:28a0/64 scope link valid_lft forever preferred_lft forever And inside a container: # ip route show default via 172.17.0.1 dev eth0 172.17.0.0/16 dev eth0 src 172.17.0.4 It's fairly easy to extract this IP address using a simple shell script: #!/bin/sh hostip=$(ip route show | awk '/default/ {print $3}') echo $hostip You may need to modify the iptables rules on your host to permit connections from Docker containers. Something like this will do the trick: # iptables -A INPUT -i docker0 -j ACCEPT This would permit access to any ports on the host from Docker containers. Note that: iptables rules are ordered, and this rule may or may not do the right thing depending on what other rules come before it. you will only be able to access host services that are either (a) listening on INADDR_ANY (aka 0.0.0.0) or that are explicitly listening on the docker0 interface. If you are using Docker on (https://docs.docker.com/docker-for-mac/networking/#there-is-no-docker0-bridge-on-macos#i-want-to-connect-from-a-container-to-a-service-on-the-host) MacOS or (https://docs.docker.com/docker-for-windows/networking/#there-is-no-docker0-bridge-on-windows#i-want-to-connect-from-a-container-to-a-service-on-the-host) Windows 18.03+, you can connect to the magic hostname host.docker.internal . Lastly, under Linux you can run your container in the host network namespace by setting --net=host ; in this case localhost on your host is the same as localhost inside the container, so containerized service will act like non-containerized services and will be accessible without any additional configuration. (https://stackoverflow.com/a/31328031) (Short permalink to this answer) Share (https://stackoverflow.com/posts/31328031/edit) Improve this answer Follow Follow this answer to receive notifications (https://stackoverflow.com/posts/31328031/revisions) (show all edits to this post) edited (2021-03-20 02:24:12Z) Mar 20, 2021 at 2:24 (https://stackoverflow.com/users/2660766/maksadbek) (maksadbek's user avatar) (https://stackoverflow.com/users/2660766/maksadbek) maksadbek (reputation score) 1,591 (2 gold badges) 2 2 gold badges (16 silver badges) 16 16 silver badges (29 bronze badges) 29 29 bronze badges answered (2015-07-09 20:54:28Z) Jul 9, 2015 at 20:54 (https://stackoverflow.com/users/147356/larsks) (larsks's user avatar) (https://stackoverflow.com/users/147356/larsks) larsks larsks (reputation score 313,955) 314k (46 gold badges) 46 46 gold badges (461 silver badges) 461 461 silver badges (471 bronze badges) 471 471 bronze badges 13 (number of 'useful comment' votes received) 3 Relevant link: (https://docs.docker.com/engine/reference/commandline/run/#add-entries-to-container-hosts-file-add-host) adding entries to container's /etc/hosts – (https://stackoverflow.com/users/209489/chronos) (443 reputation) chronos Commented (2016-03-22 10:25:28Z, License: CC BY-SA 3.0) Mar 22, 2016 at 10:25 (number of 'useful comment' votes received) 11 How about Docker for MAC? AFAIK there is no docker0 network avilable for "Docker for MAC ". In that case how can I connect to host from container? – (https://stackoverflow.com/users/674607/vijay) (365 reputation) Vijay Commented (2016-07-27 19:41:06Z, License: CC BY-SA 3.0) Jul 27, 2016 at 19:41 This works - but if you're trying to connect to a service like redis, you need to add the ip address in the ip addr show docker0 command to the bind in redis.conf and restart redis. – (https://stackoverflow.com/users/1340069/james-obrien) (1,706 reputation) James O'Brien Commented (2018-04-01 19:35:09Z, License: CC BY-SA 3.0) Apr 1, 2018 at 19:35 (number of 'useful comment' votes received) 1 I have used hostname of my host instead of getting IP address (hostname command on host) – (https://stackoverflow.com/users/7292972/marek-f) (585 reputation) Marek F Commented (2019-04-09 12:30:32Z, License: CC BY-SA 4.0) Apr 9, 2019 at 12:30 (number of 'useful comment' votes received) 1 After installing commands "ip" "dig" "ping" etc into the container (service) for testing, I have found... Ping is always sucessful, but attempted to access the published ports of another service results in "No Route to Host". However turning off iptables, and this changes to "Could not resolve host" and using the IP directly for the connection works! So I reenabled the iptables and added the appropriate ACCEPT rules, but sill not having much luck. Work continues. – (https://stackoverflow.com/users/701532/anthony) (7,885 reputation) anthony Commented (2021-04-28 23:27:27Z, License: CC BY-SA 4.0) Apr 28, 2021 at 23:27 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) | (Expand to show all comments on this post) Show 8 more comments This answer is useful 202 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/48806927/timeline) Show activity on this post. Use --net="host" in your docker run command, then localhost in your docker container will point to your docker host. (https://stackoverflow.com/a/48806927) (Short permalink to this answer) Share (https://stackoverflow.com/posts/48806927/edit) Improve this answer Follow Follow this answer to receive notifications answered (2018-02-15 12:08:02Z) Feb 15, 2018 at 12:08 (https://stackoverflow.com/users/1586965/samthebest) (samthebest's user avatar) (https://stackoverflow.com/users/1586965/samthebest) samthebest samthebest (reputation score 31,593) 31.6k (25 gold badges) 25 25 gold badges (105 silver badges) 105 105 silver badges (151 bronze badges) 151 151 bronze badges 9 (number of 'useful comment' votes received) 42 Won't work for those using Dpcker for Windows / Docker for Mac as far as I understand due to the fact that containers run in the virtualized environments: Hyper-V (Windows) / xhyve (Mac) – (https://stackoverflow.com/users/770932/ninjaboy) (300 reputation) ninjaboy Commented (2018-05-09 13:06:31Z, License: CC BY-SA 4.0) May 9, 2018 at 13:06 (number of 'useful comment' votes received) 19 THIS! This is the answer! – (https://stackoverflow.com/users/3751385/user3751385) (4,289 reputation) user3751385 Commented (2018-09-26 21:17:45Z, License: CC BY-SA 4.0) Sep 26, 2018 at 21:17 (number of 'useful comment' votes received) 45 Just for the record: within Docker Componse, network_mode: "host" – (https://stackoverflow.com/users/4058998/jbarros) (638 reputation) jbarros Commented (2019-02-09 18:12:29Z, License: CC BY-SA 4.0) Feb 9, 2019 at 18:12 (number of 'useful comment' votes received) 4 Even if I am on mac, it works when you want communication between two dockers containers and as i transform all application to docker image it is enough for me – (https://stackoverflow.com/users/4237897/bormat) (1,379 reputation) bormat Commented (2019-12-11 09:14:30Z, License: CC BY-SA 4.0) Dec 11, 2019 at 9:14 (this comment was edited 1 time) (number of 'useful comment' votes received) 26 If I understand correctly, this is not the answer, because it makes Docker use (https://docs.docker.com/network/host/) host networking : "network stack is not isolated from the Docker host (the container shares the host’s networking namespace)". Which may "work", but may not be what the OP needs or intends. – (https://stackoverflow.com/users/3951916/raketenolli) (745 reputation) Raketenolli Commented (2020-07-29 08:56:11Z, License: CC BY-SA 4.0) Jul 29, 2020 at 8:56 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) | (Expand to show all comments on this post) Show 4 more comments This answer is useful 188 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/67272342/timeline) Show activity on this post. Replace http://127.0.0.1 or http://localhost with http://host.docker.internal . (https://docs.docker.com/desktop/features/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host) Source in the docs of Docker . My google search brought me to here, and after digging in the comments I found it's a duplicate of (https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) From inside of a Docker container, how do I connect to the localhost of the machine? . I voted for closing this one as a duplicate, but since people (including myself!) often scroll down on the answers rather than reading the comments carefully, here is a short answer. (https://stackoverflow.com/a/67272342) (Short permalink to this answer) Share (https://stackoverflow.com/posts/67272342/edit) Improve this answer Follow Follow this answer to receive notifications (https://stackoverflow.com/posts/67272342/revisions) (show all edits to this post) edited (2025-02-14 13:55:16Z) Feb 14 at 13:55 (https://stackoverflow.com/users/2851664/sebix) (sebix's user avatar) (https://stackoverflow.com/users/2851664/sebix) sebix (reputation score) 3,269 (2 gold badges) 2 2 gold badges (30 silver badges) 30 30 silver badges (47 bronze badges) 47 47 bronze badges answered (2021-04-26 19:01:19Z) Apr 26, 2021 at 19:01 (https://stackoverflow.com/users/2321594/aidin) (Aidin's user avatar) (https://stackoverflow.com/users/2321594/aidin) Aidin Aidin (reputation score 30,345) 30.3k (10 gold badges) 10 10 gold badges (82 silver badges) 82 82 silver badges (81 bronze badges) 81 81 bronze badges 6 (number of 'useful comment' votes received) 2 This is a perfect solution unless you dont have to make changes locally in your code base – (https://stackoverflow.com/users/8496502/deekshith-anand) (2,722 reputation) Deekshith Anand Commented (2022-01-10 12:21:32Z, License: CC BY-SA 4.0) Jan 10, 2022 at 12:21 (this comment was edited 1 time) (number of 'useful comment' votes received) 2 Yup thats it. @DeekshithAnand , generally I'd advise structuring your code so that URLs and the like belong in environment variables (ie something like a .env file w/ appropriate loader). That way you can do stuff like this without comporomising production repeatability – (https://stackoverflow.com/users/1158644/shayne) (1,621 reputation) Shayne Commented (2022-06-27 01:19:11Z, License: CC BY-SA 4.0) Jun 27, 2022 at 1:19 (this comment was edited 1 time) (number of 'useful comment' votes received) 10 This won't work automatically, but you need to provide the following run flag: --add-host=host.docker.internal:host-gateway – (https://stackoverflow.com/users/4632019/eugen-konkov) (25,412 reputation) Eugen Konkov Commented (2022-08-06 12:24:01Z, License: CC BY-SA 4.0) Aug 6, 2022 at 12:24 (number of 'useful comment' votes received) 1 dial tcp: lookup host.docker.internal on 127.0.0.11:53: no such host – (https://stackoverflow.com/users/1850255/alexey-sh) (1,947 reputation) Alexey Sh. Commented (2023-01-19 12:21:30Z, License: CC BY-SA 4.0) Jan 19, 2023 at 12:21 (number of 'useful comment' votes received) 2 You have no idea how many hours I wasted on this just to find the concise answer, thank you. – (https://stackoverflow.com/users/12569596/kennysliding) (2,985 reputation) kennysliding Commented (2023-08-02 10:11:49Z, License: CC BY-SA 4.0) Aug 2, 2023 at 10:11 (this comment was edited 1 time) (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) | (Expand to show all comments on this post) Show 1 more comment This answer is useful 100 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/61424570/timeline) Show activity on this post. For linux systems, you can – starting from major version 20.04 of the docker engine – now also communicate with the host via host.docker.internal . This won't work automatically , but you need to provide the following run flag: --add-host=host.docker.internal:host-gateway See (https://github.com/moby/moby/pull/40007#issuecomment-578729356) https://github.com/moby/moby/pull/40007#issuecomment-578729356 (https://github.com/docker/for-linux/issues/264#issuecomment-598864064) https://github.com/docker/for-linux/issues/264#issuecomment-598864064 (https://stackoverflow.com/a/61424570) (Short permalink to this answer) Share (https://stackoverflow.com/posts/61424570/edit) Improve this answer Follow Follow this answer to receive notifications (https://stackoverflow.com/posts/61424570/revisions) (show all edits to this post) edited (2020-09-29 11:50:59Z) Sep 29, 2020 at 11:50 answered (2020-04-25 10:56:32Z) Apr 25, 2020 at 10:56 (https://stackoverflow.com/users/3757139/samuel) (Samuel's user avatar) (https://stackoverflow.com/users/3757139/samuel) Samuel Samuel (reputation score) 5,893 (6 gold badges) 6 6 gold badges (32 silver badges) 32 32 silver badges (35 bronze badges) 35 35 bronze badges 11 (number of 'useful comment' votes received) 8 For earlier Linux Docker versions we can easily work around it in a similar manner using --add-host host.docker.internal:$(ip addr show docker0 | grep -Po 'inet \K[\d.]+') – (https://stackoverflow.com/users/23023/conny) (10,162 reputation) conny Commented (2020-09-01 08:04:16Z, License: CC BY-SA 4.0) Sep 1, 2020 at 8:04 (this comment was edited 1 time) (number of 'useful comment' votes received) 2 Major version of what? – (https://stackoverflow.com/users/476371/0xc0000022l) (21,059 reputation) 0xC0000022L Commented (2020-09-16 10:41:51Z, License: CC BY-SA 4.0) Sep 16, 2020 at 10:41 (number of 'useful comment' votes received) 2 is host-gateway a keyword or does it have to be the explicit gateway of the host? hm – (https://stackoverflow.com/users/1230911/enrm) (705 reputation) enrm Commented (2020-11-09 07:50:52Z, License: CC BY-SA 4.0) Nov 9, 2020 at 7:50 (number of 'useful comment' votes received) 7 Docker Engine 20.10 was just released and with it, host-gateway should finally be available. – (https://stackoverflow.com/users/1762862/cellane) (511 reputation) Cellane Commented (2020-12-14 04:24:08Z, License: CC BY-SA 4.0) Dec 14, 2020 at 4:24 (number of 'useful comment' votes received) 1 @SzczepanHołyszewski Just tried it and YES. – (https://stackoverflow.com/users/300204/zaf) (23,292 reputation) zaf Commented (2021-07-06 11:55:45Z, License: CC BY-SA 4.0) Jul 6, 2021 at 11:55 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) | (Expand to show all comments on this post) Show 6 more comments This answer is useful 56 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/50866007/timeline) Show activity on this post. Solution with docker-compose: For accessing to host-based service, you can use network_mode parameter (https://docs.docker.com/compose/compose-file/#network_mode) https://docs.docker.com/compose/compose-file/#network_mode version: '3' services: jenkins: network_mode: host EDIT 2020-04-27: recommended for use only in local development environment. EDIT 2021-09-21: IHaveHandedInMyResignation wrote it does not work for Mac and Windows. Option is supported only for Linux (https://stackoverflow.com/a/50866007) (Short permalink to this answer) Share (https://stackoverflow.com/posts/50866007/edit) Improve this answer Follow Follow this answer to receive notifications (https://stackoverflow.com/posts/50866007/revisions) (show all edits to this post) edited (2021-09-21 14:09:08Z) Sep 21, 2021 at 14:09 answered (2018-06-14 21:11:16Z) Jun 14, 2018 at 21:11 (https://stackoverflow.com/users/2109121/vovan) (vovan's user avatar) (https://stackoverflow.com/users/2109121/vovan) vovan vovan (reputation score) 1,539 (12 silver badges) 12 12 silver badges (22 bronze badges) 22 22 bronze badges 4 (number of 'useful comment' votes received) 1 Then how to access jenkins? Seems the port forwarding is not working if use host network mode – (https://stackoverflow.com/users/769900/jeff-tian) (5,933 reputation) Jeff Tian Commented (2020-03-24 10:35:40Z, License: CC BY-SA 4.0) Mar 24, 2020 at 10:35 (number of 'useful comment' votes received) 8 it's a very risky solution and not at all recommended. we should NOT open our host network to containers unless explicitly needed – (https://stackoverflow.com/users/3366437/fatemeh-majd) (739 reputation) Fatemeh Majd Commented (2020-04-24 07:49:52Z, License: CC BY-SA 4.0) Apr 24, 2020 at 7:49 (number of 'useful comment' votes received) 2 Please edit this answer. It does not work for Mac and Windows. Option is supported only for Linux – (https://stackoverflow.com/users/7993986/ihavehandedinmyresignation) (160 reputation) IHaveHandedInMyResignation Commented (2020-11-20 13:50:03Z, License: CC BY-SA 4.0) Nov 20, 2020 at 13:50 how about windows mann – (https://stackoverflow.com/users/12716228/anas) (742 reputation) Anas Commented (2023-10-06 15:12:38Z, License: CC BY-SA 4.0) Oct 6, 2023 at 15:12 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful 29 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/51703584/timeline) Show activity on this post. I created a docker container for doing exactly that (https://github.com/qoomon/docker-host) https://github.com/qoomon/docker-host You can then simply use container name dns to access host system e.g. curl http://dockerhost:9200 (https://stackoverflow.com/a/51703584) (Short permalink to this answer) Share (https://stackoverflow.com/posts/51703584/edit) Improve this answer Follow Follow this answer to receive notifications answered (2018-08-06 08:36:00Z) Aug 6, 2018 at 8:36 (https://stackoverflow.com/users/5376635/qoomon) (qoomon's user avatar) (https://stackoverflow.com/users/5376635/qoomon) qoomon qoomon (reputation score) 5,395 (1 gold badge) 1 1 gold badge (24 silver badges) 24 24 silver badges (31 bronze badges) 31 31 bronze badges 2 (number of 'useful comment' votes received) 1 That's a clever solution. Are you aware of anything using this with a lot of traffic? There might be overhead to proxying all the traffic through this container. – (https://stackoverflow.com/users/281469/bcoughlan) (26,667 reputation) bcoughlan Commented (2018-09-04 15:31:34Z, License: CC BY-SA 4.0) Sep 4, 2018 at 15:31 (number of 'useful comment' votes received) 4 Yes it works quite nice barely no overhead at all because it just works over loopback-device – (https://stackoverflow.com/users/5376635/qoomon) (5,395 reputation) qoomon Commented (2018-09-06 13:46:11Z, License: CC BY-SA 4.0) Sep 6, 2018 at 13:46 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful 26 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/49948778/timeline) Show activity on this post. Currently the easiest way to do this on Mac and Windows is using host (https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds) host.docker.internal , that resolves to host machine's IP address. Unfortunately it (https://github.com/docker/for-linux/issues/264) does not work on linux yet (as of April 2018). (https://stackoverflow.com/a/49948778) (Short permalink to this answer) Share (https://stackoverflow.com/posts/49948778/edit) Improve this answer Follow Follow this answer to receive notifications answered (2018-04-20 19:47:18Z) Apr 20, 2018 at 19:47 (https://stackoverflow.com/users/112000/tom%c3%a1%c5%a1-fejfar) (Tomáš Fejfar's user avatar) (https://stackoverflow.com/users/112000/tom%c3%a1%c5%a1-fejfar) Tomáš Fejfar Tomáš Fejfar (reputation score 11,217) 11.2k (8 gold badges) 8 8 gold badges (57 silver badges) 57 57 silver badges (85 bronze badges) 85 85 bronze badges 3 This solution worked with Docker version 19.03.1. Many other solutions given here do not work. This is documented at (https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds) docs.docker.com/docker-for-mac/networking/… – (https://stackoverflow.com/users/1767106/clay) (20,520 reputation) clay Commented (2019-08-12 16:31:26Z, License: CC BY-SA 4.0) Aug 12, 2019 at 16:31 (number of 'useful comment' votes received) 1 this is great. ping host.docker.internal revealed the host machine's ip and I was able to connect to it. thank you! – (https://stackoverflow.com/users/38947/tamas-kalman) (1,934 reputation) Tamas Kalman Commented (2021-04-28 02:08:31Z, License: CC BY-SA 4.0) Apr 28, 2021 at 2:08 (this comment was edited 2 times) host.docker.internal isn't something setup on Windows Containers. Just tried. Even using the IP address of the host, the container network is unable to access any ports on the host. – (https://stackoverflow.com/users/557362/gilesdmiddleton) (2,320 reputation) GilesDMiddleton Commented (2024-11-27 12:07:32Z, License: CC BY-SA 4.0) Nov 27, 2024 at 12:07 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful 17 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/52206014/timeline) Show activity on this post. I've explored the various solution and I find this the least hacky solution: Define a static IP address for the bridge gateway IP. Add the gateway IP as an extra entry in the extra_hosts directive. The only downside is if you have multiple networks or projects doing this, you have to ensure that their IP address range do not conflict. Here is a Docker Compose example: version: '2.3' services: redis: image: "redis" extra_hosts: - "dockerhost:172.20.0.1" networks: default: ipam: driver: default config: - subnet: 172.20 .0 .0 /16 gateway: 172.20 .0 .1 You can then access ports on the host from inside the container using the hostname "dockerhost". (https://stackoverflow.com/a/52206014) (Short permalink to this answer) Share (https://stackoverflow.com/posts/52206014/edit) Improve this answer Follow Follow this answer to receive notifications (https://stackoverflow.com/posts/52206014/revisions) (show all edits to this post) edited (2020-11-06 00:12:39Z) Nov 6, 2020 at 0:12 (https://stackoverflow.com/users/1402846/pang) (Pang's user avatar) (https://stackoverflow.com/users/1402846/pang) Pang (reputation score 10,134) 10.1k (146 gold badges) 146 146 gold badges (86 silver badges) 86 86 silver badges (124 bronze badges) 124 124 bronze badges answered (2018-09-06 14:09:11Z) Sep 6, 2018 at 14:09 (https://stackoverflow.com/users/281469/bcoughlan) (bcoughlan's user avatar) (https://stackoverflow.com/users/281469/bcoughlan) bcoughlan bcoughlan (reputation score 26,667) 26.7k (20 gold badges) 20 20 gold badges (96 silver badges) 96 96 silver badges (148 bronze badges) 148 148 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful 16 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/56027620/timeline) Show activity on this post. We found that a simpler solution to all this networking junk is to just use the domain socket for the service. If you're trying to connect to the host anyway, just mount the socket as a volume, and you're on your way. For postgresql, this was as simple as: docker run -v /var/run/postgresql:/var/run/postgresql Then we just set up our database connection to use the socket instead of network. Literally that easy. (https://stackoverflow.com/a/56027620) (Short permalink to this answer) Share (https://stackoverflow.com/posts/56027620/edit) Improve this answer Follow Follow this answer to receive notifications answered (2019-05-07 17:19:37Z) May 7, 2019 at 17:19 (https://stackoverflow.com/users/64911/mlissner) (mlissner's user avatar) (https://stackoverflow.com/users/64911/mlissner) mlissner mlissner (reputation score 18,246) 18.2k (18 gold badges) 18 18 gold badges (121 silver badges) 121 121 silver badges (176 bronze badges) 176 176 bronze badges 1 (number of 'useful comment' votes received) 5 FYI, we ran into a big issue with this: Docker for Mac doesn't support sockets as mounted volumes. This went swimmingly until a Mac person tried it. :( – (https://stackoverflow.com/users/64911/mlissner) (18,246 reputation) mlissner Commented (2019-09-12 22:22:41Z, License: CC BY-SA 4.0) Sep 12, 2019 at 22:22 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful 15 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/59554205/timeline) Show activity on this post. For docker-compose using bridge networking to create a private network between containers, the accepted solution using docker0 doesn't work because the egress interface from the containers is not docker0 , but instead, it's a randomly generated interface id, such as: $ ifconfig br-02d7f5ba5a51: flags=4163 mtu 1500 inet 192.168.32.1 netmask 255.255.240.0 broadcast 192.168.47.255 Unfortunately that random id is not predictable and will change each time compose has to recreate the network (e.g. on a host reboot). My solution to this is to create the private network in a known subnet and configure iptables to accept that range: Compose file snippet: version: "3.7" services: mongodb: image: mongo:4.2.2 networks: - mynet # rest of service config and other services removed for clarity networks: mynet: name: mynet ipam: driver: default config: - subnet: "192.168.32.0/20" You can change the subnet if your environment requires it. I arbitrarily selected 192.168.32.0/20 by using docker network inspect to see what was being created by default. Configure iptables on the host to permit the private subnet as a source: $ iptables -I INPUT 1 -s 192.168.32.0/20 -j ACCEPT This is the simplest possible iptables rule. You may wish to add other restrictions, for example by destination port. Don't forget to persist your iptables rules when you're happy they're working. This approach has the advantage of being repeatable and therefore automatable. I use ansible's template module to deploy my compose file with variable substitution and then use the iptables and shell modules to configure and persist the firewall rules, respectively. (https://stackoverflow.com/a/59554205) (Short permalink to this answer) Share (https://stackoverflow.com/posts/59554205/edit) Improve this answer Follow Follow this answer to receive notifications (https://stackoverflow.com/posts/59554205/revisions) (show all edits to this post) edited (2020-11-06 00:13:54Z) Nov 6, 2020 at 0:13 (https://stackoverflow.com/users/1402846/pang) (Pang's user avatar) (https://stackoverflow.com/users/1402846/pang) Pang (reputation score 10,134) 10.1k (146 gold badges) 146 146 gold badges (86 silver badges) 86 86 silver badges (124 bronze badges) 124 124 bronze badges answered (2020-01-01 16:37:00Z) Jan 1, 2020 at 16:37 (https://stackoverflow.com/users/1763035/andy-brown) (Andy Brown's user avatar) (https://stackoverflow.com/users/1763035/andy-brown) Andy Brown Andy Brown (reputation score 13,069) 13.1k (2 gold badges) 2 2 gold badges (52 silver badges) 52 52 silver badges (71 bronze badges) 71 71 bronze badges 4 (number of 'useful comment' votes received) 3 I saw multiple answers suggesting iptables -A INPUT -i docker0 -j ACCEPT , but that didn't help me, whereas the iptables -I INPUT 1 -s 192.168.32.0/20 -j ACCEPT suggested here solved my problem. – (https://stackoverflow.com/users/1072212/terry-brown) (1,340 reputation) Terry Brown Commented (2020-06-09 16:47:39Z, License: CC BY-SA 4.0) Jun 9, 2020 at 16:47 That is the only proper thing that works. – (https://stackoverflow.com/users/1271135/mikhail-orlov) (2,857 reputation) Mikhail Orlov Commented (2021-12-07 16:28:02Z, License: CC BY-SA 4.0) Dec 7, 2021 at 16:28 (number of 'useful comment' votes received) 1 Maybe a better way on Ubuntu is sudo ufw allow from 192.168.32.0/20 . That persists restarts. – (https://stackoverflow.com/users/1271135/mikhail-orlov) (2,857 reputation) Mikhail Orlov Commented (2021-12-08 12:40:06Z, License: CC BY-SA 4.0) Dec 8, 2021 at 12:40 It doesn't help me – (https://stackoverflow.com/users/12073621/adam) (625 reputation) Adam Commented (2022-09-23 14:06:27Z, License: CC BY-SA 4.0) Sep 23, 2022 at 14:06 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful 7 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/60836732/timeline) Show activity on this post. This is an old question and had many answers, but none of those fit well enough to my context. In my case, the containers are very lean and do not contain any of the networking tools necessary to extract the host's ip address from within the container. Also, usin the --net="host" approach is a very rough approach that is not applicable when one wants to have well isolated network configuration with several containers. So, my approach is to extract the hosts' address at the host's side, and then pass it to the container with --add-host parameter: $ docker run --add-host=docker-host:`ip addr show docker0 | grep -Po 'inet \K[\d.]+'` image_name or, save the host's IP address in an environment variable and use the variable later: $ DOCKERIP=`ip addr show docker0 | grep -Po 'inet \K[\d.]+'` $ docker run --add-host=docker-host:$DOCKERIP image_name And then the docker-host is added to the container's hosts file, and you can use it in your database connection strings or API URLs. (https://stackoverflow.com/a/60836732) (Short permalink to this answer) Share (https://stackoverflow.com/posts/60836732/edit) Improve this answer Follow Follow this answer to receive notifications answered (2020-03-24 17:59:08Z) Mar 24, 2020 at 17:59 (https://stackoverflow.com/users/640694/passiday) (Passiday's user avatar) (https://stackoverflow.com/users/640694/passiday) Passiday Passiday (reputation score) 8,125 (11 gold badges) 11 11 gold badges (45 silver badges) 45 45 silver badges (65 bronze badges) 65 65 bronze badges 1 This also allows you to add a specific host, in case you are talking to Apache and you want to speak to a very specific host. --add-host local.acme.com:$DOCKERIP – (https://stackoverflow.com/users/611526/bruno-bossola) (1,378 reputation) Bruno Bossola Commented (2021-08-31 13:57:01Z, License: CC BY-SA 4.0) Aug 31, 2021 at 13:57 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful 5 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/62147846/timeline) Show activity on this post. For me (Windows 10, Docker Engine v19.03.8) it was a mix of (https://stackoverflow.com/a/43541732/7924573) https://stackoverflow.com/a/43541732/7924573 and (https://stackoverflow.com/a/50866007/7924573) https://stackoverflow.com/a/50866007/7924573 . change the host/ip to host.docker.internal e.g.: LOGGER_URL = "(http://host.docker.internal:8085/log) http://host.docker.internal:8085/log " set the network_mode to bridge (if you want to maintain the port forwarding; if not use host ):version: '3.7' services: server: build: . ports: - "5000:5000" network_mode: bridge or alternatively: Use --net="bridge" if you are not using docker-compose (similar to (https://stackoverflow.com/a/48806927/7924573) https://stackoverflow.com/a/48806927/7924573 )As pointed out in previous answers: This should only be used in a local development environment .For more information read: (https://docs.docker.com/compose/compose-file/#network_mode) https://docs.docker.com/compose/compose-file/#network_mode and (https://docs.docker.com/docker-for-windows/networking/#use-cases-and-workarounds) https://docs.docker.com/docker-for-windows/networking/#use-cases-and-workarounds (https://stackoverflow.com/a/62147846) (Short permalink to this answer) Share (https://stackoverflow.com/posts/62147846/edit) Improve this answer Follow Follow this answer to receive notifications answered (2020-06-02 08:27:17Z) Jun 2, 2020 at 8:27 (https://stackoverflow.com/users/7924573/tschomacker) (tschomacker's user avatar) (https://stackoverflow.com/users/7924573/tschomacker) tschomacker tschomacker (reputation score) 824 (11 silver badges) 11 11 silver badges (22 bronze badges) 22 22 bronze badges 1 host.docker.internal solved the issue for me. Allowed me to access a dev postgres from another container that did not have container access to the other container. DATABASE_URL=jdbc:postgresql://host.docker.internal:5432/mirthdb – (https://stackoverflow.com/users/3424487/michael-hobbs) (1,691 reputation) Michael Hobbs Commented (2020-08-16 16:21:23Z, License: CC BY-SA 4.0) Aug 16, 2020 at 16:21 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful 4 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/55585883/timeline) Show activity on this post. You can access the local webserver which is running in your host machine in two ways. Approach 1 with public IP Use host machine public IP address to access webserver in Jenkins docker container. Approach 2 with the host network Use "--net host" to add the Jenkins docker container on the host's network stack. Containers which are deployed on host's stack have entire access to the host interface. You can access local webserver in docker container with a private IP address of the host machine. NETWORK ID NAME DRIVER SCOPE b3554ea51ca3 bridge bridge local 2f0d6d6fdd88 host host local b9c2a4bc23b2 none null local Start a container with the host network Eg: docker run --net host -it ubuntu and run ifconfig to list all available network IP addresses which are reachable from docker container. Eg: I started a nginx server in my local host machine and I am able to access the nginx website URLs from Ubuntu docker container. docker run --net host -it ubuntu $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a604f7af5e36 ubuntu "/bin/bash" 22 seconds ago Up 20 seconds ubuntu_server Accessing the Nginx web server (running in local host machine) from Ubuntu docker container with private network IP address. root@linuxkit-025000000001:/# curl 192.168.x.x -I HTTP/1.1 200 OK Server: nginx/1.15.10 Date: Tue, 09 Apr 2019 05:12:12 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 26 Mar 2019 14:04:38 GMT Connection: keep-alive ETag: "5c9a3176-264" Accept-Ranges: bytes (https://stackoverflow.com/a/55585883) (Short permalink to this answer) Share (https://stackoverflow.com/posts/55585883/edit) Improve this answer Follow Follow this answer to receive notifications answered (2019-04-09 05:47:55Z) Apr 9, 2019 at 5:47 (https://stackoverflow.com/users/11331676/sreenivasa-reddy) (Sreenivasa Reddy's user avatar) (https://stackoverflow.com/users/11331676/sreenivasa-reddy) Sreenivasa Reddy Sreenivasa Reddy (reputation score) 304 (1 silver badge) 1 1 silver badge (7 bronze badges) 7 7 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful 3 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/70870433/timeline) Show activity on this post. In almost 7 years the question was asked, it is either docker has changed, or no one tried this way. So I will include my own answer. I have found all answers use complex methods. Today, I have needed this, and found 2 very simple ways: use ipconfig or ifconfig on your host and make note of all IP addresses. At least two of them can be used by the container. I have a fixed local network address on WiFi LAN Adapter: 192.168.1.101 . This could be 10.0.1.101 . the result will change depending on your router I use WSL on windows, and it has its own vEthernet address: 172.19.192.1 use host.docker.internal . Most answers have this or another form of it depending on OS. The name suggests it is now globally used by docker. A third option is to use WAN address of the machine, or in other words IP given by the service provider. However, this may not work if IP is not static, and requires routing and firewall settings. PS: Although pretty identical to (https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach/70870343#70870343) this question here , and I posted this answer there, I first found this post, so I post it here too as may forget my own answer. (https://stackoverflow.com/a/70870433) (Short permalink to this answer) Share (https://stackoverflow.com/posts/70870433/edit) Improve this answer Follow Follow this answer to receive notifications answered (2022-01-26 21:23:56Z) Jan 26, 2022 at 21:23 (https://stackoverflow.com/users/9512475/y%c4%b1lmaz-durmaz) (Yılmaz Durmaz's user avatar) (https://stackoverflow.com/users/9512475/y%c4%b1lmaz-durmaz) Yılmaz Durmaz Yılmaz Durmaz (reputation score) 3,054 (18 silver badges) 18 18 silver badges (36 bronze badges) 36 36 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful 2 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/67216536/timeline) Show activity on this post. The simplest option that worked for me was, I used the IP address of my machine on the local network(assigned by the router) You can find this using the ifconfig command e.g ifconfig en0: flags=8863 mtu 1500 options=400 ether f0:18:98:08:74:d4 inet 192.168.178.63 netmask 0xffffff00 broadcast 192.168.178.255 media: autoselect status: active and then used the inet address. This worked for me to connect any ports on my machine. (https://stackoverflow.com/a/67216536) (Short permalink to this answer) Share (https://stackoverflow.com/posts/67216536/edit) Improve this answer Follow Follow this answer to receive notifications answered (2021-04-22 15:41:33Z) Apr 22, 2021 at 15:41 (https://stackoverflow.com/users/11460723/amrut-prabhu) (Amrut Prabhu's user avatar) (https://stackoverflow.com/users/11460723/amrut-prabhu) Amrut Prabhu Amrut Prabhu (reputation score) 1,539 (13 silver badges) 13 13 silver badges (13 bronze badges) 13 13 bronze badges 1 ifconfig doesn't appear to be a valid windows command - did you have to install somethign? – (https://stackoverflow.com/users/557362/gilesdmiddleton) (2,320 reputation) GilesDMiddleton Commented (2024-11-27 12:11:49Z, License: CC BY-SA 4.0) Nov 27, 2024 at 12:11 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful -4 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/40642961/timeline) Show activity on this post. When you have two docker images "already" created and you want to put two containers to communicate with one-another. For that, you can conveniently run each container with its own --name and use the --link flag to enable communication between them. You do not get this during docker build though. When you are in a scenario like myself, and it is your docker build -t "centos7/someApp" someApp/ That breaks when you try to curl http://172.17.0.1:localPort/fileIWouldLikeToDownload.tar.gz > dump.tar.gz and you get stuck on "curl/wget" returning no "route to host". The reason is security that is set in place by docker that by default is banning communication from a container towards the host or other containers running on your host. This was quite surprising to me, I must say, you would expect the echosystem of docker machines running on a local machine just flawlessly can access each other without too much hurdle. The explanation for this is described in detail in the following documentation. (http://www.dedoimedo.com/computers/docker-networking.html) http://www.dedoimedo.com/computers/docker-networking.html Two quick workarounds are given that help you get moving by lowering down the network security. The simplest alternative is just to turn the firewall off - or allow all. This means running the necessary command, which could be systemctl stop firewalld, iptables -F or equivalent. (https://stackoverflow.com/a/40642961) (Short permalink to this answer) Share (https://stackoverflow.com/posts/40642961/edit) Improve this answer Follow Follow this answer to receive notifications (https://stackoverflow.com/posts/40642961/revisions) (show all edits to this post) edited (2022-03-14 09:52:57Z) Mar 14, 2022 at 9:52 (https://stackoverflow.com/users/4685471/desertnaut) (desertnaut's user avatar) (https://stackoverflow.com/users/4685471/desertnaut) desertnaut (reputation score 60,494) 60.5k (32 gold badges) 32 32 gold badges (155 silver badges) 155 155 silver badges (182 bronze badges) 182 182 bronze badges answered (2016-11-16 21:47:23Z) Nov 16, 2016 at 21:47 (https://stackoverflow.com/users/171956/99sono) (99Sono's user avatar) (https://stackoverflow.com/users/171956/99sono) 99Sono 99Sono (reputation score) 3,697 (33 silver badges) 33 33 silver badges (42 bronze badges) 42 42 bronze badges 1 (number of 'useful comment' votes received) 4 Just as a note,--link is now deprecated – (https://stackoverflow.com/users/4504862/mr-budris) (562 reputation) Mr.Budris Commented (2017-09-22 19:49:58Z, License: CC BY-SA 3.0) Sep 22, 2017 at 19:49 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) Start asking to get answers Find the answer to your question by asking. (https://stackoverflow.com/questions/ask) Ask question Explore related questions (https://stackoverflow.com/questions/tagged/docker) (show questions tagged 'docker') docker (https://stackoverflow.com/questions/tagged/docker-container) (show questions tagged 'docker-container') docker-container See similar questions with these tags. The Overflow Blog (https://stackoverflow.blog/2025/04/23/community-products-roadmap-update-april-2025/?cb=1) Community Products roadmap update, April 2025 (https://stackoverflow.blog/2025/04/25/grab-bag-on-the-floor-at-humanx/?cb=1) Grab bag! On the floor at HumanX Featured on Meta (Meta Stack Exchange) (https://meta.stackexchange.com/questions/408408/results-of-the-march-2025-community-asks-sprint?cb=1) Results of the March 2025 Community Asks Sprint (Meta Stack Exchange) (https://meta.stackexchange.com/questions/408476/upcoming-initiatives-on-stack-overflow-and-across-the-stack-exchange-network-a?cb=1) (Upcoming initiatives on Stack Overflow and across the Stack Exchange network - April 2025) Upcoming initiatives on Stack Overflow and across the Stack Exchange network... (Meta Stack Overflow) (https://meta.stackoverflow.com/questions/421831/policy-generative-ai-e-g-chatgpt-is-banned?cb=1) Policy: Generative AI (e.g., ChatGPT) is banned (Meta Stack Overflow) (https://meta.stackoverflow.com/questions/433641/what-we-learned-about-follow-up-questions-and-whats-next?cb=1) What We Learned About Follow-up Questions and What's Next Linked (https://stackoverflow.com/q/24319662?lq=1) (Question score (upvotes - downvotes)) 3533 (https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach?noredirect=1&lq=1) From inside of a Docker container, how do I connect to the localhost of the machine? (https://stackoverflow.com/q/55479787?lq=1) (Question score (upvotes - downvotes)) 0 (https://stackoverflow.com/questions/55479787/how-to-configure-spring-boot-microservice-docker-container-to-access-local-mongo?noredirect=1&lq=1) How to configure spring-boot microservice docker container to access local mongodb database? (https://stackoverflow.com/q/52350205?lq=1) (Question score (upvotes - downvotes)) 1 (https://stackoverflow.com/questions/52350205/guzzle-http-from-docker-to-nodejs-in-host?noredirect=1&lq=1) Guzzle HTTP from Docker to NodeJS in Host (https://stackoverflow.com/q/55605479?lq=1) (Question score (upvotes - downvotes)) 0 (https://stackoverflow.com/questions/55605479/access-web-application-running-on-host-on-docker-container?noredirect=1&lq=1) Access Web Application running on host on Docker Container (https://stackoverflow.com/q/63653049?lq=1) (Question score (upvotes - downvotes)) 0 (https://stackoverflow.com/questions/63653049/how-would-i-connect-to-a-mysql-on-the-host-machine-from-inside-a-docker-kubernet?noredirect=1&lq=1) How would I connect to a MySQL on the host machine from inside a docker kubernetes pod? (https://stackoverflow.com/q/65648295?lq=1) (Question score (upvotes - downvotes)) 0 (https://stackoverflow.com/questions/65648295/connect-to-a-local-postgresql-database-from-a-docker-compose-service?noredirect=1&lq=1) Connect to a local PostgreSQL database from a docker-compose service (https://stackoverflow.com/q/22944631?lq=1) (Question score (upvotes - downvotes)) 581 (https://stackoverflow.com/questions/22944631/how-to-get-the-ip-address-of-the-docker-host-from-inside-a-docker-container?noredirect=1&lq=1) How to get the IP address of the docker host from inside a docker container (https://stackoverflow.com/q/48546124?lq=1) (Question score (upvotes - downvotes)) 407 (https://stackoverflow.com/questions/48546124/what-is-the-linux-equivalent-of-host-docker-internal?noredirect=1&lq=1) What is the Linux equivalent of "host.docker.internal"? (https://stackoverflow.com/q/32163955?lq=1) (Question score (upvotes - downvotes)) 239 (https://stackoverflow.com/questions/32163955/how-to-run-shell-script-on-host-from-docker-container?noredirect=1&lq=1) How to run shell script on host from docker container? (https://stackoverflow.com/q/42385977?lq=1) (Question score (upvotes - downvotes)) 227 (https://stackoverflow.com/questions/42385977/accessing-a-docker-container-from-another-container?noredirect=1&lq=1) Accessing a Docker container from another container (https://stackoverflow.com/questions/linked/31324981?lq=1) See more linked questions Related (https://stackoverflow.com/q/22049212?rq=1) (Question score (upvotes - downvotes)) 3005 (https://stackoverflow.com/questions/22049212/copying-files-from-docker-container-to-host?rq=1) Copying files from Docker container to host (https://stackoverflow.com/q/22907231?rq=1) (Question score (upvotes - downvotes)) 2568 (https://stackoverflow.com/questions/22907231/how-to-copy-files-from-host-to-docker-container?rq=1) How to copy files from host to Docker container? (https://stackoverflow.com/q/16047306?rq=1) (Question score (upvotes - downvotes)) 4490 (https://stackoverflow.com/questions/16047306/how-is-docker-different-from-a-virtual-machine?rq=1) How is Docker different from a virtual machine? (https://stackoverflow.com/q/30172605?rq=1) (Question score (upvotes - downvotes)) 2432 (https://stackoverflow.com/questions/30172605/how-do-i-get-into-a-docker-containers-shell?rq=1) How do I get into a Docker container's shell? (https://stackoverflow.com/q/23935141?rq=1) (Question score (upvotes - downvotes)) 2412 (https://stackoverflow.com/questions/23935141/how-to-copy-docker-images-from-one-host-to-another-without-using-a-repository?rq=1) How to copy Docker images from one host to another without using a repository (https://stackoverflow.com/q/17157721?rq=1) (Question score (upvotes - downvotes)) 2246 (https://stackoverflow.com/questions/17157721/how-to-get-a-docker-containers-ip-address-from-the-host?rq=1) How to get a Docker container's IP address from the host (https://stackoverflow.com/q/23735149?rq=1) (Question score (upvotes - downvotes)) 1254 (https://stackoverflow.com/questions/23735149/what-is-the-difference-between-a-docker-image-and-a-container?rq=1) What is the difference between a Docker image and a container? (https://stackoverflow.com/q/28056522?rq=1) (Question score (upvotes - downvotes)) 241 (https://stackoverflow.com/questions/28056522/access-host-database-from-a-docker-container?rq=1) Access host database from a docker container (https://stackexchange.com/questions?tab=hot) Hot Network Questions (Politics Stack Exchange) (https://politics.stackexchange.com/questions/91513/why-is-trump-signing-many-more-executive-orders-during-his-second-term-than-duri) Why is Trump signing many more executive orders during his second term than during his first term? (German Language Stack Exchange) (https://german.stackexchange.com/questions/79143/undeclined-modifiers-used-with-adjectival-nouns) Undeclined modifiers used with adjectival nouns (Mathematics Stack Exchange) (https://math.stackexchange.com/questions/5059607/mathematically-precise-statement-and-proof-of-maxwell-relations) Mathematically precise statement and proof of Maxwell relations? (English Language & Usage Stack Exchange) (https://english.stackexchange.com/questions/631059/a-single-word-for-dishonestly-underselling-ones-own-importance-credentials) A single word for dishonestly underselling one's own importance/credentials? (Mathematica Stack Exchange) (https://mathematica.stackexchange.com/questions/312394/curved-edgestyle-in-graph) Curved edgestyle in Graph (Travel Stack Exchange) (https://travel.stackexchange.com/questions/195430/im-staying-in-the-us-for-a-night-at-the-beginning-and-end-of-my-trip-did-i-nee) I'm staying in the US for a night at the beginning and end of my trip. Did I need to select the transit option on the ESTA? (TeX - LaTeX Stack Exchange) (https://tex.stackexchange.com/questions/741603/shortstack-vertical-line-spacing-not-uniform) Shortstack vertical line spacing not uniform (Bicycles Stack Exchange) (https://bicycles.stackexchange.com/questions/96671/spoke-tension-question) Spoke tension question (Law Stack Exchange) (https://law.stackexchange.com/questions/108453/a-married-pope) A married pope? (Electrical Engineering Stack Exchange) (https://electronics.stackexchange.com/questions/744728/subtractor-circuit-issue-on-ltspice-whats-wrong) Subtractor circuit issue on LTSpice: what's wrong? (Role-playing Games Stack Exchange) (https://rpg.stackexchange.com/questions/215362/how-to-deal-with-scheduling-issues-in-a-small-group-of-players) How to deal with scheduling issues in a small group of players? (Cross Validated) (https://stats.stackexchange.com/questions/664638/is-backpropagation-faulty) Is Backpropagation faulty? (Blender Stack Exchange) (https://blender.stackexchange.com/questions/333908/create-a-3d-helical-spiral-based-on-phi-with-constant-radius-using-geometry-node) Create a 3D helical spiral based on PHI with constant radius using geometry nodes (The Great Outdoors Stack Exchange) (https://outdoors.stackexchange.com/questions/30167/multiday-hike-in-uk-with-children) Multiday hike in UK with children (Academia Stack Exchange) (https://academia.stackexchange.com/questions/218120/accepting-a-postdoc-over-a-tt-position) Accepting a Postdoc over a TT Position (Stack Overflow) (https://stackoverflow.com/questions/79594753/why-use-a-mutex-and-not-a-semaphore) Why use a mutex and not a semaphore? (Mathematica Stack Exchange) (https://mathematica.stackexchange.com/questions/312378/best-numerical-scheme-for-solving-an-equation-with-several-variables) Best numerical scheme for solving an equation with several variables (Chemistry Stack Exchange) (https://chemistry.stackexchange.com/questions/188508/difference-in-magnetic-moment-of-criii-and-coii-ions-despite-having-the-sam) Difference in magnetic moment of Cr(III) and Co(II) ions, despite having the same number of unpaired electrons (Academia Stack Exchange) (https://academia.stackexchange.com/questions/218104/grade-grubbing-students-escalating-to-deans) Grade-grubbing students escalating to deans (Role-playing Games Stack Exchange) (https://rpg.stackexchange.com/questions/215360/how-to-reconcile-old-school-tropes-with-modern-sensibilities) How to reconcile old school tropes with modern sensibilities? (Cross Validated) (https://stats.stackexchange.com/questions/664616/result-in-opposite-direction-to-hypothesis-one-or-two-tailed-p-value) Result in opposite direction to hypothesis - one or two-tailed p-value? (Cryptography Stack Exchange) (https://crypto.stackexchange.com/questions/114914/was-the-higher-cipher-that-robert-graves-describes-in-his-novel-i-claudius) Was the "higher cipher" that Robert Graves describes in his novel "I, Claudius" a real historical cipher, or was it his invention? (English Language Learners Stack Exchange) (https://ell.stackexchange.com/questions/364569/she-looked-as-if-she-would-be-sick) She looked as if she would be sick (Seasoned Advice) (https://cooking.stackexchange.com/questions/130521/jello-without-the-entire-box-of-jello-mix) Jello without the entire box of Jello mix? () (https://stackoverflow.com/) (https://stackoverflow.com/) Stack Overflow (https://stackoverflow.com/questions) Questions (https://stackoverflow.com/help) Help (https://chat.stackoverflow.com/?tab=site&host=stackoverflow.com) Chat (https://stackoverflow.co/) Products (https://stackoverflow.co/teams/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=teams) Teams (https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=advertising) Advertising (https://stackoverflow.co/advertising/employer-branding/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=talent) Talent (https://stackoverflow.co/) Company (https://stackoverflow.co/) About (https://stackoverflow.co/company/press/) Press (https://stackoverflow.co/company/work-here/) Work Here (https://stackoverflow.com/legal) Legal (https://stackoverflow.com/legal/privacy-policy) Privacy Policy (https://stackoverflow.com/legal/terms-of-service/public) Terms of Service (https://stackoverflow.com/contact) Contact Us Your Privacy Choices (https://stackoverflow.com/legal/cookie-policy) Cookie Policy (https://stackexchange.com/) Stack Exchange Network (https://stackexchange.com/sites#technology) Technology (https://stackexchange.com/sites#culturerecreation) Culture & recreation (https://stackexchange.com/sites#lifearts) Life & arts (https://stackexchange.com/sites#science) Science (https://stackexchange.com/sites#professional) Professional (https://stackexchange.com/sites#business) Business (https://api.stackexchange.com/) API (https://data.stackexchange.com/) Data (https://stackoverflow.blog/?blb=1) Blog (https://www.facebook.com/officialstackoverflow/) Facebook (https://twitter.com/stackoverflow) Twitter (https://linkedin.com/company/stack-overflow) LinkedIn (https://www.instagram.com/thestackoverflow) Instagram Site design / logo © 2025 Stack Exchange Inc; user contributions licensed under (https://stackoverflow.com/help/licensing) CC BY-SA . rev 2025.4.24.25623 By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our (https://stackoverflow.com/legal/cookie-policy) Cookie Policy . Accept all cookies Necessary cookies only Customize settings