I ran into a problem with an ASP.NET 5 Web API project running as a docker container -- It started fine, and I could hit the api using curl from inside the container, but not from a separate, linked container. I was really thrown for a loop by this, because I could ping the ASP.NET container, and with curl it showed and empty response rather than an "unreachable/blocked port" response.

It turns out that by default ASP.NET Core 1.0 (or, more specifically, WebHostBuilder()) only listens for requests on the localhost address 127.0.0.1.

In other words, it will only work if the connecting process is on the "same machine" network-wise, since that would be the only way to connect to 127.0.0.1. This was probably provided as the most secure option for a default configuration. However, in Docker, each container gets its own IP address, so it doesn't work by default.

This is different from many other HTTP server implementations' default configurations, for example, node's http.Server and go's net/http.

The solution to this problem is detailed on this Stack Overflow post: How Do I get the Kestrel Server to listen to non-localhost requests?

    var host = new WebHostBuilder()
        .UseUrls("http://*:1000", "https://*:1234", "http://0.0.0.0:5000")
        .UseEnvironment("Development")
        .UseConfiguration(config)
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

Where .UseUrls("http://*:1000", "https://*:1234", "http://0.0.0.0:5000") is the key line.

http://localhost:5000 is the default if no UseUrls() is specified.

http://*:5000 would configure the app to listen for network traffic on any interface for any host on port 5000, which is more in line with what I would expect.

Comments