I have done my first example with oclet with docker. I have 2 images 1 is my ocelot apigateway running on localhost:6002 and one is my api running on localhost:6001
` my.api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://0.0.0.0:80
- ConnectionString=${AuthenticationDB_PROD:-Server=sql.data;Database=AuthenticationDb;User Id=SA;Password=Pass@word}
- UseCustomizationData=True
- AzureServiceBusEnabled=False
- AzureStorageEnabled=False
ports:
- "6001:80"
apigateway.api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://0.0.0.0:80
- UseCustomizationData=True
- AzureServiceBusEnabled=False
- AzureStorageEnabled=False
ports:
- "6002:80"`
Here my config:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{version}/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 6001
}
],
"UpstreamPathTemplate": "/api/auth/{version}/{everything}",
"UpstreamHttpMethod": []
}
]
If I run http://localhost:6001/api/v1/authentication/authenticate everything works fine. But if I call this endpoint trough the apigateway I see in the logs the following error
Ocelot.Responder.Middleware.ResponderMiddleware[0]
requestId: 0HLLV201IGIHI:0000000C, previousRequestId: no previous request id, message: Error Code: UnableToCompleteRequestError Message: Error making http request, exception: System.Net.Http.HttpRequestException: Cannot assign requested address ---> System.Net.Sockets.SocketException: Cannot assign requested address
I see that ocelot is trying to call this url and calling this with postman it works fine
Downstream url is http://localhost:6001/api/v1/authentication/authenticate
I think I made some beginner error but I can't find him
Thanks
I'm also experiencing this issue. This is specifically with a dockerized gateway. I have run a standard gateway and can get it to reach the apis fine. with an exact duplicate config.json.
it seems to be resolving the downstream urls correctly, I can copy and paste them from the console into postman and they run just fine.
Here's my config.json
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/PropertySearch/{url}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 60357
}
],
"UpstreamPathTemplate": "/PropertySearch/{url}",
"UpstreamHttpMethod": [ "Get" ]
}
],
"GlobalConfiguration": {
"RequestIdKey": "OcRequestId",
"AdministrationPath": "/administration"
}
}
And my error logs.
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:53148/PropertySearch/GetProperty/LS1 3AD
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:53148/PropertySearch/GetProperty/LS1 3AD
Ocelot.Errors.Middleware.ExceptionHandlerMiddleware:Debug: requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: ocelot pipeline started
[40m[37mdbug[39m[22m[49m: Ocelot.Errors.Middleware.ExceptionHandlerMiddleware[0]
requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: ocelot pipeline started
Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware:Debug: requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: Upstream url path is /PropertySearch/GetProperty/LS1%203AD
[40m[37mdbug[39m[22m[49m: Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware[0]
requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: Upstream url path is /PropertySearch/GetProperty/LS1%203AD
Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware:Debug: requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: downstream templates are /api/PropertySearch/{url}
[40m[37mdbug[39m[22m[49m: Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware[0]
requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: downstream templates are /api/PropertySearch/{url}
[40m[32minfo[39m[22m[49m: Ocelot.RateLimit.Middleware.ClientRateLimitMiddleware[0]
requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: EndpointRateLimiting is not enabled for /api/PropertySearch/{url}
Ocelot.RateLimit.Middleware.ClientRateLimitMiddleware:Information: requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: EndpointRateLimiting is not enabled for /api/PropertySearch/{url}
[40m[32minfo[39m[22m[49m: Ocelot.Authentication.Middleware.AuthenticationMiddleware[0]
requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: No authentication needed for /PropertySearch/GetProperty/LS1%203AD
Ocelot.Authentication.Middleware.AuthenticationMiddleware:Information: requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: No authentication needed for /PropertySearch/GetProperty/LS1%203AD
Ocelot.Authorisation.Middleware.AuthorisationMiddleware:Information: requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: /api/PropertySearch/{url} route does not require user to be authorised
[40m[32minfo[39m[22m[49m: Ocelot.Authorisation.Middleware.AuthorisationMiddleware[0]
requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: /api/PropertySearch/{url} route does not require user to be authorised
[40m[37mdbug[39m[22m[49m: Ocelot.DownstreamUrlCreator.Middleware.DownstreamUrlCreatorMiddleware[0]
requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: Downstream url is http://localhost:60357/api/PropertySearch/GetProperty/LS1%203AD
Ocelot.DownstreamUrlCreator.Middleware.DownstreamUrlCreatorMiddleware:Debug: requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: Downstream url is http://localhost:60357/api/PropertySearch/GetProperty/LS1%203AD
[40m[37mdbug[39m[22m[49m: Ocelot.Requester.Middleware.HttpRequesterMiddleware[0]
requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: IHttpRequester returned an error, setting pipeline error
Ocelot.Requester.Middleware.HttpRequesterMiddleware:Debug: requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: IHttpRequester returned an error, setting pipeline error
Ocelot.Requester.Middleware.HttpRequesterMiddleware:Warning: requestId: 0HLLV2V8LEGC4:00000006, previousRequestId: no previous request id, message: Error making http request, exception: System.Net.Http.HttpRequestException: Cannot assign requested address ---> System.Net.Sockets.SocketException: Cannot assign requested address
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask1 creationTask)
at System.Threading.Tasks.ValueTask1.get_Result()
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Ocelot.Requester.HttpClientHttpRequester.GetResponse(DownstreamContext context)`
I have also disabled https to try to simplify the issue further. On both the apis and the gateway.
Any suggestions?
I think it has something to do with the docker routing. Might be worth trying to point to the actual container IP while this issue gets fixed.
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
(gets you the docker container ip)
I had a similar problem, i think docker has an internal DNS server or something and "localhost" doesn't mean the same thing to a native and a dockerized app.
Anyway, if you are using docker-compose you can use their service names as their ip addresses.
So in the ocelot.conf file insted of:
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 6001
}
],
you write,
"DownstreamHostAndPorts": [
{
"Host": "my.api",
"Port": 6001
}
],
Hope it hepls.
Most helpful comment
I had a similar problem, i think docker has an internal DNS server or something and "localhost" doesn't mean the same thing to a native and a dockerized app.
Anyway, if you are using docker-compose you can use their service names as their ip addresses.
So in the ocelot.conf file insted of:
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 6001
}
],
you write,
"DownstreamHostAndPorts": [
{
"Host": "my.api",
"Port": 6001
}
],
Hope it hepls.