0

I've got a window service that exposes ports for reasons. I'm getting it to run in a container, but I can't seem to connect to the application outside of the container. I run the container like so:

docker run --net=nat -p:8080:8080 [container] 

In the code where the server listens it uses

    _listener = new TcpListener((IPEndPoint)listenEndPoint);//localhost,8080

If I attach to the container and run a powershell command to confirm the port is open and listening, it works. This is the powershell command:

New-Object System.Net.Sockets.TcpClient("127.0.0.1", 8080)

Is there some more configuration I need to do to get the connectivity working?

Edit: In the same service, where I'm listening for TCP clients via the code above, I host a WCF endpoint. That resolves fine.

Irwin
  • 12,551
  • 11
  • 67
  • 97
  • 2
    Did you try using `0.0.0.0` instead of `localhost`? – trust512 Mar 27 '19 at 16:38
  • I get an exception with this message when I do that: *IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address* – Irwin Mar 27 '19 at 21:51

1 Answers1

0

The container's loopback interface is different from that of the host. If you are running services on the container, you should have it bind to "0.0.0.0"; this will ensure that the container is actually listening to the port it is bound to on the host machine.

Frank Yucheng Gu
  • 1,807
  • 7
  • 21
  • I get an exception with this message when I do that: *IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address* – Irwin Mar 27 '19 at 21:48
  • @Irwin No, this is not your target address. Your target address **outside** of the container shall be `localhost`. **Inside** your container, your listening/binding on `0.0.0.0`. – Frank Yucheng Gu Mar 28 '19 at 02:04
  • Yep. That doesn't work. The server inside the container doesn't let you specify 0.0.0.0 as it's listening address. – Irwin Mar 28 '19 at 14:47
  • 1
    Ah, try to use the [Any](https://learn.microsoft.com/fr-fr/dotnet/api/system.net.ipaddress.any?redirectedfrom=MSDN&view=netframework-4.7.2) address – Frank Yucheng Gu Mar 28 '19 at 14:56
  • Thanks, Frank, I used Any and it worked. I did a small script to test the effect here: https://gist.github.com/irwinwilliams/575a1fe6481ab09af8d21f11ffb89835 – Irwin Apr 22 '19 at 09:58