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 |2 Answers
Reset to default 1I'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
http://localhost:6000/v1.0/invoke/web4/method/Ping
for checking. – Jason Pan Commented Mar 25 at 6:56