Docker Community Forums

Share and learn in the Docker community.

HttpListener and connecting to container from host


(Cliftonm) #1

I’ve been banging my head against this for a while, searching various forums, etc. Anyways, as an experiment, I set up a docker container with .NET 4.7 framework using Docker For Windows (Hyper-V is enabled, etc), and wrote a simple HttpListener server. Basically:

webServer.Start("http://*:5001/");
        public void Start(string ip)
        {
            listener = new HttpListener();
            listener.Prefixes.Add(ip);
            listener.Start();
            ... wait for connection, etc...

My Dockerfile:

FROM microsoft/dotnet-framework:4.7
WORKDIR /app
COPY bin/Debug .
EXPOSE 5001/tcp
ENTRYPOINT ["test2.exe"]

And it’s run like this:

docker run -p 5001:5001 test2

No runtime errors are reported, and of course it works fine running native on my W10 box. The big question is, how can I access this simple server running in the container?

I’ve read various posts, some saying it’s not possible, others saying this needs to be done through a VM instead, but the various posts I’ve read seem contradictory. Ideas?

[edit]

To sort of answer my own question, I came across this:

It appears that you have to proxy localhost to the local IP of the container. Is this the only way to get localhost to work on the host machine? Certainly the container’s local IP works, but that’s somewhat annoying, as the container IP changes every time the container app is started.

[/edit]


(Sam) #2

from outside the windows box you would use http://windows_box_ip_address:5001
and of course you need the windows firewall to allow incoming connections to 5001.