I have a gateway that maps multiple services. This works great when I run it without docker. However, when I change to the docker version, it just doesn’t work.
I get the following message:
: POST’
2024-10-21 06:21:49 [10:21:49 WRN] requestId: 0HN7H3M32VGVE:00000001,
previousRequestId: No PreviousRequestId, message: ‘Error Code:
ConnectionToDownstreamServiceError Message: Error connecting to
downstream service, exception: System.Net.Http.HttpRequestException:
Connection refused (crm:8001) 2024-10-21 06:21:49 —>
System.Net.Sockets.SocketException (111): Connection refused
2024-10-21 06:21:49 at
System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError
error, CancellationToken cancellationToken) 2024-10-21 06:21:49 at
System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16
token) 2024-10-21 06:21:49 at
System.Net.Sockets.Socket.g__WaitForConnectWithCancellation|285_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
2024-10-21 06:21:49 at
System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host,
Int32 port, HttpRequestMessage initialRequest, Boolean async,
CancellationToken cancellationToken) 2024-10-21 06:21:49 — End of
inner exception stack trace — 2024-10-21 06:21:49 at
System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host,
Int32 port, HttpRequestMessage initialRequest, Boolean async,
CancellationToken cancellationToken) 2024-10-21 06:21:49 at
System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage
request, Boolean async, CancellationToken cancellationToken)
2024-10-21 06:21:49 at
System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage
request, Boolean async, CancellationToken cancellationToken)
2024-10-21 06:21:49 at
System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem
queueItem) 2024-10-21 06:21:49 at
System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken
cancellationToken) 2024-10-21 06:21:49 at
System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage
request, Boolean async, Boolean doRequestAuth, CancellationToken
cancellationToken) 2024-10-21 06:21:49 at
System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage
request, Boolean async, CancellationToken cancellationToken)
2024-10-21 06:21:49 at
Ocelot.Requester.TimeoutDelegatingHandler.SendAsync(HttpRequestMessage
request, CancellationToken cancellationToken) 2024-10-21 06:21:49
at
Ocelot.Requester.MessageInvokerHttpRequester.GetResponse(HttpContext
httpContext) errors found in ResponderMiddleware. Setting error
response for request path:/crm/customers/get-page, request method:
POST’
I have changed the ocelot.json from this:
{
"Routes": [
// CRM routes
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8001
}
],
"UpstreamPathTemplate": "/crm/{everything}",
"UpstreamHttpMethod": [ "GET", "POST" ]
}
//other mappings
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost:5000;https://localhost:5001"
}
}
to:
{
"Routes": [
// CRM routes
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "crm",
"Port": 8001
}
],
"UpstreamPathTemplate": "/crm/{everything}",
"UpstreamHttpMethod": [ "GET", "POST" ]
}
// other mappings.
"GlobalConfiguration": {
"BaseUrl": "http://localhost:5000;https://localhost:5001"
}
}
Why would this not be able to connect to the docker services. They all work correctly from postman.
2
Answers
I found the issue, I hope this helps others. I was trying to connect to the external port of the docker container. In other words the port that we map to when I should have been mapped to the port that we expose in the dockerfile.
Try adding a
network
in your Docker Compose file and ensure that all services are using the same network, or updateDownstreamHostAndPorts
to use the container IP instead of the hostname.