

The Easy Optionĭocker Desktop 18.03+ for Windows and Mac supports as a functioning alias for localhost.
#Docker network=host how to#
Here’s how to access localhost or 127.0.0.1 from within a Docker container.

Sometimes you might need a container to talk to a service on your host that hasn’t been containerized. This is a complex problem, and many of the steps in the way are better solved with using pre-allocated ports on the invoking docker command-line with -p which is kind of why they designed its common-use-case to use that method in the first place.When working with Docker, you usually containerize the services that form your stack and use inter-container networking to communicate between them. What then? Crash your container or return a “port already open” syscall error? Now the behavior is still different from Linux, because sometimes the OSX-housed container will barf at start up claiming “port already open” when you’re opening port 0, which is supposed to assign an unused port before opening it, and thus should never barf up “port already open” unless the machine has already exhausted all available ports. Now, the Linux machine is stuck telling the host “hey, open up port 64435 and redirect it to me” and the host saying “no can do, it’s already open for a different program”. You might find it frustrating, but it is not a show-stopper, it is in fact, something that they cannot make work without instrumenting up the open TCP socket syscall in the Linux virtual guest, AND setting up a bridged network in the first place, and even then, ok, the container opens port 0, which is “random unprivileged port”, the Linux machine opens up port 64435 on it’s virtual linux host, but a different process running on the machine’s OSX host already has port 64435 (like, say a web-browser) open. (Otherwise, it can’t track the opened ports.) Is there really anything they can do without essentially setting up a bridged NAT in the first place, and just replicating -p? No, there really isn’t.ĭocker was designed with the intent that -p is the way things will work. Is the behavior inconsistent? Yes, it is. So, now, considering that the docker daemon doesn’t even know what port was eventually opened by the container, how would you expect the OSX invoker to even know? How could this work with -net=host? Docker in this case even the docker daemon running on the virtualized Linux machine doesn’t even actually know which port the container opened, because the container has done an “end run” around docker straight to the namespaced kernel. The invoking agent on the Mac knows exactly which port needs to be redirected and how.

It is not docker running on the Linux machine that is instigating this port opening.

Yes, a -p works so well, because it’s opening a localport that is actually a redirect to an address that is NATed behind the docker bridged networking. I also tried this with pinata set network nat and pinata set network hostnet– neither works. The service doesn’t need to be python3- any native app that binds to localhost will work. I also tried every address known to the container: $ docker run -it -rm -net=host buildpack-deps:curl sh -c 'for ip in $(ip addr | grep "inet " | cut -d " " -f 6 | cut -d "/" -f 1) do curl $:8000 done'Ĭurl: (7) Failed to connect to 127.0.0.1 port 8000: Connection refusedĬurl: (7) Failed to connect to 192.168.65.2 port 8000: Connection refusedĬurl: (7) Failed to connect to 172.19.0.1 port 8000: Connection refusedĬurl: (7) Failed to connect to 172.18.0.1 port 8000: Connection refusedĬurl: (7) Failed to connect to 172.17.0.1 port 8000: Connection refused On OSX, the output is simply curl: (7) Failed to connect to 127.0.0.1 port 8000: Connection refused
