asp.net core - Issue running Dapr Sidecar using docker compose in .NET project - Stack Overflow

ContextI've created an ASP.NET Core 8 Web API project, added DAPR client and DAPR.ASPNETCORE to i

Context

I've created an ASP.NET Core 8 Web API project, added DAPR client and DAPR.ASPNETCORE to it, created a PingController. When I run this on my local machine using this command:

dapr run --app-id web4 --app-port 5001 --dapr-http-port 5002 -- dotnet run --urls http://+:5001
  • I can see my web service is running Swagger

  • I can see the sidecar is running on dapr dashboard on default port 8080

  • I can hit URL:

    GET http://localhost:5002/v1.0/invoke/web4/method/Ping
    

    and in the response, I can see pong

I am trying to run this with docker-compose.

Here are the files :

Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers().AddDapr();
// Learn more about configuring Swagger/OpenAPI at 
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseCloudEvents();
app.MapSubscribeHandler();

app.UseAuthorization();

app.MapControllers();

app.Run();

PingController.cs:

using Microsoft.AspNetCore.Mvc;

namespace WebApplication4.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class PingController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            return Ok("pong");
        }
    }
}

docker-compose.yml:

services:
  webapplication4:
    image: ${DOCKER_REGISTRY-}webapplication4
    build:
      context: .
      dockerfile: WebApplication4/Dockerfile
    ports:
      - "5000:5001"
    volumes:
      - ./aspnet/https:/https:ro
    networks:
      - webapp-network

  webapplication4-api-dapr:
    image: "daprio/daprd:latest"
    command: ["./daprd", 
        "--app-id", "web4", 
        "--app-port", "5001", 
        "--app-protocol", "http",
        "--dapr-http-port", "6001"
    ]
    ports:
      - "6000:6001"
    depends_on:
      - webapplication4
    volumes:
      - ./aspnet/https:/https:ro
    networks:
      - webapp-network

  dapr-dashboard:
    image: "daprio/dashboard:latest"
    command: dapr run && dapr dashboard --port 8080
    # command: ["dapr", "run", "dashboard", "--port", "8080"]
    ports:
      - "8989:8080"
    networks:
      - webapp-network

networks:
  webapp-network:
    driver: bridge

docker-compose.override.yml:

services:
  webapplication4:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_HTTP_PORTS=5000
      - ASPNETCORE_HTTPS_PORTS=6000
    ports:
      - "5000"
      - "6000"
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro

Issues and questions

When I hit run with docker-compose, all my services starts running in Docker:

  • I can see my web service with swagger running on port 56005 instead of 5000 - why this port? I don't know. Each time it runs on a random port ! But it is working correctly
  • I can see Dapr dashboard running inside container and it is available on my local port of 8989, but there is no sidecar running

When I try to hit URL:

GET http://localhost:6000/v1.0/invoke/web4/method/Ping

I get this response :

{
    "errorCode": "ERR_DIRECT_INVOKE",
    "message": "failed to invoke, id: web4, err: Get \"http://127.0.0.1:5001/Ping\": dial tcp 127.0.0.1:5001: connect: connection refused"
}

What can I do to fix it?

Context

I've created an ASP.NET Core 8 Web API project, added DAPR client and DAPR.ASPNETCORE to it, created a PingController. When I run this on my local machine using this command:

dapr run --app-id web4 --app-port 5001 --dapr-http-port 5002 -- dotnet run --urls http://+:5001
  • I can see my web service is running Swagger

  • I can see the sidecar is running on dapr dashboard on default port 8080

  • I can hit URL:

    GET http://localhost:5002/v1.0/invoke/web4/method/Ping
    

    and in the response, I can see pong

I am trying to run this with docker-compose.

Here are the files :

Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers().AddDapr();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseCloudEvents();
app.MapSubscribeHandler();

app.UseAuthorization();

app.MapControllers();

app.Run();

PingController.cs:

using Microsoft.AspNetCore.Mvc;

namespace WebApplication4.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class PingController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            return Ok("pong");
        }
    }
}

docker-compose.yml:

services:
  webapplication4:
    image: ${DOCKER_REGISTRY-}webapplication4
    build:
      context: .
      dockerfile: WebApplication4/Dockerfile
    ports:
      - "5000:5001"
    volumes:
      - ./aspnet/https:/https:ro
    networks:
      - webapp-network

  webapplication4-api-dapr:
    image: "daprio/daprd:latest"
    command: ["./daprd", 
        "--app-id", "web4", 
        "--app-port", "5001", 
        "--app-protocol", "http",
        "--dapr-http-port", "6001"
    ]
    ports:
      - "6000:6001"
    depends_on:
      - webapplication4
    volumes:
      - ./aspnet/https:/https:ro
    networks:
      - webapp-network

  dapr-dashboard:
    image: "daprio/dashboard:latest"
    command: dapr run && dapr dashboard --port 8080
    # command: ["dapr", "run", "dashboard", "--port", "8080"]
    ports:
      - "8989:8080"
    networks:
      - webapp-network

networks:
  webapp-network:
    driver: bridge

docker-compose.override.yml:

services:
  webapplication4:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_HTTP_PORTS=5000
      - ASPNETCORE_HTTPS_PORTS=6000
    ports:
      - "5000"
      - "6000"
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro

Issues and questions

When I hit run with docker-compose, all my services starts running in Docker:

  • I can see my web service with swagger running on port 56005 instead of 5000 - why this port? I don't know. Each time it runs on a random port ! But it is working correctly
  • I can see Dapr dashboard running inside container and it is available on my local port of 8989, but there is no sidecar running

When I try to hit URL:

GET http://localhost:6000/v1.0/invoke/web4/method/Ping

I get this response :

{
    "errorCode": "ERR_DIRECT_INVOKE",
    "message": "failed to invoke, id: web4, err: Get \"http://127.0.0.1:5001/Ping\": dial tcp 127.0.0.1:5001: connect: connection refused"
}

What can I do to fix it?

Share Improve this question edited Mar 25 at 9:43 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Mar 25 at 5:41 Fenil PatelFenil Patel 475 bronze badges 2
  • Hi @Fenil Patel, please try to use my modified docker-compose.override.yml , and access the url http://localhost:6000/v1.0/invoke/web4/method/Ping for checking. – Jason Pan Commented Mar 25 at 6:56
  • Hi @JasonPan Thanks, this did solved the problem of "my app service not running on correct port". but for dapr sidecar hit issue is still the same, for which i needed to move the 'ports' from dapr-sidecar to webApp in compose file and needed to set the 'network_mode' same as service for dapr-sidecar. I've posted the answer on my own question, you can checkout. Thanks for the help. – Fenil Patel Commented Mar 25 at 9:33
Add a comment  | 

2 Answers 2

Reset to default 1

I've got the solution for swagger not running on correct port and for dapr sidecar hit issue as well :

apprently I needed to expose dapr sidecar port on my service application and had to use network_mode for dapr service in yaml file of docker-compose,

and needed to expose that port in docker-compose.override.yaml file.

here is the change to do so :

docker-compose.yaml : (check ports and network_mode)

services:
  webapplication4:
    image: ${DOCKER_REGISTRY-}webapplication4
    build:
      context: .
      dockerfile: WebApplication4/Dockerfile
    ports:
      - "5000:5001"
      - "6000:6001"
    volumes:
      - ./aspnet/https:/https:ro
    networks:
      - webapp-network

  webapplication4-api-dapr:
    image: "daprio/daprd:latest"
    command: ["./daprd", 
        "--app-id", "web4", 
        "--app-port", "5001", 
        "--app-protocol", "https",
        "--dapr-http-port", "6001"
    ]
    depends_on:
      - webapplication4
    volumes:
      - ./aspnet/https:/https:ro
    network_mode: "service:webapplication4"

networks:
  webapp-network:
    driver: bridge

docker-compose.override.yaml :

services:
  webapplication4:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_HTTPS_PORTS=5001
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro

Another solution --

scene-service-dapr:
    image: "daprio/daprd:latest"
    command: [
      "./daprd",
      "--app-id", "scene-service",
      "--app-port", "5004",
      "--app-channel-address", "scene-service", //add channel address 
      "--dapr-http-port", "3500",
      "--dapr-grpc-port", "50001",
      "--components-path", "/components"
    ]
    ports:
      - "3500:3500"  # Expose Dapr HTTP port
      - "50001:50001"  # Expose Dapr gRPC port
    depends_on:
      - scene-service
    volumes:
      - "./components/:/components"
      - ./aspnet/https:/https:ro
    networks:
      - mynetwork

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744216102a4563561.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信