Given:
Problem
Now the Core Application wants to access HTTP GET http://localhost/foo
var client = new HttpClient();
var result = await client.GetAsync("http://localhost/foo");
But all requests to http://localhost are failing with
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.CurlException: Couldn't connect to server at System.Net.Http.CurlHandler.ThrowIfCURLEError(CURLcode error) at System.Net.Http.CurlHandler.MultiAge...
Is this expected? I thought access from docker container to localhost container should work.
Also other protocols like mongodb are working with localhost. So I can connect to a mongodb://localhost without any problem .
So it seems to be specific to http client?
any advices? Is this a more general docker topic or as I assume a asp core http client issue?
Requests for example with using my PC IP or external ip's are working as expected
@Gentlehag
Try using http://* instead of http://localhost - we've been using that in Docker with great success.
Sry i don't understand . When I try
client.GetAsync("http://*/foo")
it tells me that the hostname could not be parsed
System.UriFormatException: 'Invalid URI: The hostname could not be parsed.'
When you register the url's during the Program.cs Main method, setting UseUrls for the WebHostBuilder - define it to be http://*
can you give me a short example ? and do you mean to define it in the applciation in the docker container or the one i'm calling on the physical machine?
the following should work:
//program.cs
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
Ensure Dockerfile contains: (expose the port and use that one)
EXPOSE 8080/tcp
ENV ASPNETCORE_URLS https://*:8080
@Gentlehag, did you solved your problem? I am facing the same issue you described.
edit: My understanding of docker network was wrong. After more studies I could understand that call localhost did not make any sense in my situation.
Let me first clarify: .NET Core isn't doing anything special with Docker networking. The same question would likely apply to other types of apps, like Go or Java.
@Gentlehag you said localhost works for Mongo. Where is that running? Also in a docker container? Or is this running on Windows?
Requests for example with using my PC IP or external ip's are working as expected
Yes, this is the best way to get to IIS. "localhost" is the Docker container itself, or the Docker host if you run in "host" mode. Because you are running Linux containers on Windows, the "host" is a Linux VM which is accessed over an internal, virtual network switch (typically a Hyper-V internal network). In short, your Docker container is running two virtual networking stacks above IIS.
You've encountered a commonly asked question (see https://github.com/docker/docker/issues/1143 from 4 years ago), and there are dozens of blogs and StackOverflow answers on the subject. Here's one for starters: http://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach
This issue is being closed because it has not been updated in 3 months.
We apologize if this causes any inconvenience. We ask that if you are still encountering this issue, please log a new issue with updated information and we will investigate.
Possible solution: https://github.com/docker/for-win/issues/204#issuecomment-535187856
Most helpful comment
the following should work:
Ensure Dockerfile contains: (expose the port and use that one)