.Net Core Dockerized WebAPI failing to register with Eureka server

I have a .Net Core 3.1 WebAPI running as a Docker container + Eureka server running as Docker also. When I run the API from Visual Studio it registers successfully with the Eureka Docker instance and I can see the service in Eureka console. But once I dockerize the API and run it I am getting connection error as below :

fail: Steeltoe.Discovery.Eureka.Transport.EurekaHttpClient[0]
productsapi_1   |       RegisterAsync Failed
productsapi_1   | System.Net.Http.HttpRequestException: Connection refused
productsapi_1   |  ---> System.Net.Sockets.SocketException (111): Connection refused
productsapi_1   |    at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
productsapi_1   |    --- End of inner exception stack trace ---
productsapi_1   |    at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
......
productsapi_1   |    at Steeltoe.Discovery.Eureka.Transport.EurekaHttpClient.RegisterAsync(InstanceInfo info)
productsapi_1   | fail: Steeltoe.Discovery.Eureka.DiscoveryClient[0]
productsapi_1   |       Register Failed
productsapi_1   | System.Net.Http.HttpRequestException: Connection refused
productsapi_1   |  ---> System.Net.Sockets.SocketException (111): Connection refused
productsapi_1   |    at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
productsapi_1   |    --- End of inner exception stack trace ---
productsapi_1   |    at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
.....
productsapi_1   |    at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
productsapi_1   |    at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean 
disposeCts)
productsapi_1   |    at Steeltoe.Discovery.Eureka.Transport.EurekaHttpClient.RegisterAsync(InstanceInfo info)
productsapi_1   |    at Steeltoe.Discovery.Eureka.DiscoveryClient.RegisterAsync()
productsapi_1   | info: Steeltoe.Discovery.Eureka.DiscoveryClient[0]
productsapi_1   |       Registartion fail. HeartBeat not start
productsapi_1   | fail: Steeltoe.Discovery.Eureka.Transport.EurekaHttpClient[0]
productsapi_1   |       DoGetApplicationsAsync Failed
productsapi_1   | System.Net.Http.HttpRequestException: Connection refused
productsapi_1   |  ---> System.Net.Sockets.SocketException (111): Connection refused
.....

This is after running docker-compose up.Eureaka and API containers are both up but the service does not seem to be registering.
This is how my WebAPI looks like :

Startup.cs

 public void ConfigureServices(IServiceCollection services)
    {
        .....       
        services.AddDbContext<ApplicationDbContext>(o => o.UseMySql(Configuration.GetConnectionString("mysqlconnection")));
        services.AddDiscoveryClient(Configuration);
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {        
       .....
        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
        app.UseDiscoveryClient();
    }

Docker compose

version: '3.4'
services:
  productsapi:
    build: .
    ports: 
      - "5050:5000"
    depends_on:
      - eurekaserver
   
  eurekaserver:
    image: steeltoeoss/eureka-server:latest
    ports:
      - '8761:8761'

Appsettings.json

     {
     ...
  "AllowedHosts": "*",
  ...
  "eureka": {
    "client": {
      "shouldRegisterWithEureka": true,
      "serviceUrl": "http://eurekaserver:8761/eureka",
      "ValidateCertificates": false
    },
    "instance": {
      "appName": "ProductService",
      "port": "5050",
      "hostName": "localhost"
    }
  }
}

What am I missing ?

Source: Docker Questions

LEAVE A COMMENT