Bad Gateway Error when use yarp in docker
I get a 504 error when running yarp on docker and calling other services.
appsettings.json
docker-compose.yml
http://localhost:5201/users
http://localhost:5101/products
http://localhost:5029/users-service/users
There is no problem in code test mode and the result is displayed
Collect Debug level logs for more detail: https://microsoft.github.io/reverse-proxy/articles/diagnosing-yarp-issues.html#logging
logs in YarpGateway Container when use Reverse Proxy
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Connection: keep-alive
Host: localhost:5029
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Upgrade-Insecure-Requests: 1
sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Linux"
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
{"RouteId":"users-route","Match":{"Methods":null,"Hosts":null,"Path":"/users-service/{**catch-all}","QueryParameters":null,"Headers":null},"Order":null,"ClusterId":"users-cluster","AuthorizationPolicy":null,"RateLimiterPolicy":null,"CorsPolicy":null,"MaxRequestBodySize":null,"Metadata":null,"Transforms":[{"PathPattern":"{**catch-all}"}]}
info: Yarp.ReverseProxy.Forwarder.HttpForwarder[9]
Proxying to http://localhost:5199/Home/Privacy HTTP/2 RequestVersionOrLower no-streaming
info: Yarp.ReverseProxy.Forwarder.HttpForwarder[48]
Request: An error was encountered before receiving a response.
System.Net.Http.HttpRequestException: Cannot assign requested address (localhost:5201)
---> System.Net.Sockets.SocketException (99): Cannot assign requested address
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|281_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem queueItem)
at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.HttpConnectionWaiter`1.WaitForConnectionAsync(Boolean async, CancellationToken requestCancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at Yarp.ReverseProxy.Forwarder.HttpForwarder.SendAsync(HttpContext context, String destinationPrefix, HttpMessageInvoker httpClient, ForwarderRequestConfig requestConfig, HttpTransformer transformer, CancellationToken cancellationToken)
System.Net.Sockets.SocketException (99): Cannot assign requested address that's a new one. It doesn't seem to be a YARP issue, but a docker issue. The internet has some interesting suggestions like https://medium.com/it-dead-inside/docker-containers-and-localhost-cannot-assign-requested-address-6ac7bc0d042b.
Thank you for your help. I changed this part of the code and my problem was solved
"Clusters": {
"users-cluster": {
"Destinations": {
"destination1": {
"Address": "http://host.docker.internal:5201"
}
}
},
"products-cluster": {
"Destinations": {
"destination1": {
"Address": "http://host.docker.internal:5101"
}
}
}
}
Thank you for your help. I changed this part of the code and my problem was solved
"Clusters": { "users-cluster": { "Destinations": { "destination1": { "Address": "http://host.docker.internal:5201" } } }, "products-cluster": { "Destinations": { "destination1": { "Address": "http://host.docker.internal:5101" } } } }
What if instead of the 'host.docker.internal' you put the acutal container names within docker? This should work correct?
"Address": "http://users:5201"
Milan does it in his YARP video:
https://youtu.be/UidT7YYu97s?si=hG6TCoXL7tt70f1G&t=486