Docker : how to (reverse) forward port to allow access to the host’s localhost:PORT?


I want to enable a docker VM to connect to a port on the localhost of the host starting docker on windows.

docker proposes port forwarding but IMHO only to expose a docker’s VM port to the host running the VM.

It’s also possible to map a hostname to an IP address when starting docker, but again, that is only seen on the VM.
So neither of the following options help:

docker run  
    --add-host host: 
    -p 6000:6000 

My current purpose is to expose the XServer port running on the Windows host to the VM . My current solution is to find a Windows host IP, but when the network changes that IP is no longer valid and the XServer is apparently not listening on the new IP.
This happens when the computer hibernates and is on a new network when it wakes up.

By comparison, ssh provides the -L and -R options that allows to forward an IP/port either way.
A possible solution could be to start the container in detached mode, ssh to it and setup the port forwarding, and do all the other work in another docker run.
Another possible solution is to setup a NAT server (I didn’t try).
Using host.docker.internal in the VM may be a solution – I start testing this to see if it holds over time. It does work for the initial connection (I set DISPLAY to host.docker.internal:0.0). (host-gateway may need more investigation).
Run without isolation (option --net host). Not tested.

So my question is, what method(s) are there to allow the docker VM to access a port on the Windows HOST’s localhost addresses (127.0.X.X) .

A less general question is: how to setup the DISPLAY/Xserver on a Windows host and docker runs so that they always work, even after changing networks.

Note that my host OS is Windows 10 (this might be easier to do on *nix).

Source: Docker Questions

Categorised as display, docker, portforwarding, virtual-machine, xserver Tagged , , , ,


Leave a Reply

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us