runtime: http request in docker container fails with curl error

Near the end of converting a webapi 2 service to aspcore I’ve run up against a problem hosting the aspcore service in docker. The problem is that no HttpRequest are working anymore in the project. They throw the follow exception:

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.MultiAgent.FinishRequest(StrongToWeakReference`1 easyWrapper, CURLcode messageResult)
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)

And they only throw this exception when running inside the docker container FROM microsoft/dotnet:1.0-runtime and FROM microsoft/dotnet:1.1.1-runtime This exception doesn’t occur on the host machine running docker and has been no problem so far as I rebuild the project piece by in a new solution. I believe this issue might have started after migrating to csproj from project.json.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 33 (16 by maintainers)

Most upvoted comments

I’m facing same issue. Does anyone have solution?

	[ERROR]An error occurred while sending the request.
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.MultiAgent.FinishRequest(StrongToWeakReference`1 easyWrapper, CURLcode messageResult)
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsyncUnbuffered>d__59.MoveNext()

Hi guys,

I can confirm this is a proxy-related issue. Thanks for all the help!

Running into the same problem, curl from within the container works but not HttpClient which produces

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.MultiAgent.FinishRequest(StrongToWeakReference`1 easyWrapper, CURLcode messageResult)
var _httpClient = new HttpClient
            {
                BaseAddress = new Uri("http://api") 
                // api is the name of the service in docker-compose, which should resolve by the container automatically
            };

var response = await _httpClient.PostAsJsonAsync(
                    "api/v1/log", logList);
                    // effectively posting to http://api/api/v1/logList
services:
  api:
    image: loggingapi
    build:
      context: .
      dockerfile: LoggingAPI/Dockerfile
    ports:
      - "3002:80"

  client:
    image: clientsite
    build:
      context: .
      dockerfile: ClientSite/Dockerfile
    ports:
      - "3001:80"

The port 80 shouldn’t matter but posting to both http://api and http://api:80 didn’t work. The exposed port 3002 is intended for external access not for inter-container communications.