8

I've updated docker on my os x 10.10, so it's now using os x native virtualization. However, I've found it tricky to connect to my host machine from within my nginx container. I tried this:

/sbin/ip route|awk '/default/ { print $3 }'

And got the answer:

172.17.0.1

Then I used this ip in docker-compose.yml:

  extra_hosts:
    - "master:172.17.0.1" 

But nonetheless I keep getting errors:

172.17.0.1 - - [21/Jul/2016:09:33:46 +0000] "GET /api HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36" "-" 2016/07/21 09:33:46 [error] 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: soc-credit.ru, request: "GET /api HTTP/1.1", upstream: "http://172.17.0.5:8080/api", host: "localhost"

Please note this part: client: 172.17.0.1. Since I've made request from host machine, it proves that ip I got in first step was correct. But connection wasn't established anyway.

I want to stress out that I have a problem connecting FROM WITHIN container TO host and not vice versa.

What am I doing wrong? Thank you!

3
  • Possible duplicate of Docker Beta on Mac : Cannot use ip to access nginx container
    – ldg
    Commented Jul 21, 2016 at 13:17
  • It's not really "native" - Mac doesn't have a Linux kernel, it's just more seamless and uses xhyve instead of VB and uses Unix sockets for communication so according to the docs "Unfortunately, due to limtations in OSX, we’re unable to route traffic to containers, and from containers back to the host." See here and here.
    – ldg
    Commented Jul 21, 2016 at 13:22
  • I suppose that questioner have difficulties with connecting to container from the host machine. Or maybe I've mistaken?
    – Skeeve
    Commented Jul 21, 2016 at 13:28

4 Answers 4

15

Had the same problem and being inexperienced in Docker and network configurations I struggled to get the various proposed solutions working. However since this update to Docker for Mac: Docker Community Edition 17.06.0-ce-mac18, 2017-06-28 (stable) I've found using the 'experimental' hostname docker.for.mac.localhost allows me to contact services running on the Mac host from within a container. Very useful for dev!

1
  • 1
    replacing localhost with docker.for.mac.localhost in connection urls solved it for me. Thanks for the short solution Commented Jun 8, 2018 at 7:08
9

I am having the same problem and I've found this

https://docs.docker.com/docker-for-mac/networking/#/known-limitations-use-cases-and-workarounds

Notice paragraph 'I want to connect from a container to a service on the host'

I haven't yet tried it because I am looking for a more concrete solution (anyway other than adding alias to my lo address)...

If somebody has something on it please inform!

4
  • 1
    Well, I actually returned to docker-machine, but for people who decided against that we found an arguable solution: $(ipconfig getifaddr en0). I don't want to post it as an answer because it's a hard to use with docker-compose and more of it, after you change network you'll need to restart.
    – Skeeve
    Commented Feb 1, 2017 at 13:43
  • 1
    @Skeeve Because I use the docker-compose (in most of my services) I gave in to what the document's proposal and used sudo ifconfig lo0 alias 10.200.10.1/24. The con (among maybe others) is that in development, in case of reboot you need to set again the alias.
    – kostia
    Commented Feb 2, 2017 at 7:52
  • I like this solution so far. You can add this to .bashrc or .zshrc and it will work just fine. I am more concerned about possible conflicts on lo0 interface. Is it possible?
    – Skeeve
    Commented Feb 2, 2017 at 9:07
  • 1
    possible conflicts would be if you are trying to access directly to the IP 10.200.10.1 and it is not your host. You won't have issues accessing remote addresses with their URL (I guess).
    – kostia
    Commented Feb 3, 2017 at 9:01
3

From docker 18.03 onwards official recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host, this is for development purpose, refer to https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

0
2

latest:DNS name host.docker.internal should be used for host resolution from containers. Older aliases (still valid) are deprecated in favor of this one. (See https://datatracker.ietf.org/doc/html/draft-west-let-localhost-be-localhost-06). docker.for.mac.host.internal should be used instead of docker.for.mac.localhost from Docker Community Edition 17.12.0-ce-mac46 2018-01-09. this allows you to connect to service running on your on mac from within a docker container.please refer below links

understanding the docker.for.mac.localhost behavior

release notes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.