Search code examples
asp.net-coredocker-composecontainersdapr

Dapr and Docker Compose guidance


I'm going round in circles trying to work out how to do this but I have the following situation. I am trying to incorporate Dapr into my ASP.NET Core microservices.

I have installed Dapr locally by following the documentation (dapr init) and as a result I have three containers running locally (dapr_redis, dapr_placement and dapr_zipkin) which I can see via Docker Desktop (and via docker ps).

I have a number of ASP.NET core microservices to run, but each service is in their own visual studio solution ... that is I don't have one solution containing all my microservice projects.

My question is, how can I use Docker Compose to deploy (and debug) a single microservice? I'm trying to work out how to inject the Dapr sidecar into my microservice, but also have my microservice reference the existing, and already running, Dapr containers for Redis, Placement, etc. Is this possible? I've tried to follow the documentation to the best of my ability but I can't seem to get it working. My compose file currently looks like this:

version: '3.4'

services:

  microservicea:
    image: ${DOCKER_REGISTRY-}microservicea
    build:
      context: .
      dockerfile: MicroserviceA/Dockerfile
    #depends_on:
    #  - redis
    #  - placement
  microservicea-dapr:
    image: "daprio/daprd:edge"
    command: ["./daprd",
      "-app-id", "microservicea",
      "-app-port", "3000",
      "-placement-host-address", "placement:50005",
      "-dapr-grpc-port", "50002",
      "-components-path", "/components"]
    depends_on:
      - microservicea
    network_mode: "service:microservicea"

If I'm going about this wrong, what is the best way to handle this scenario?

Ultimately I'm trying to cater for a situation where I am working on Microservice A, but this service makes a call to Microservice B (Dapr service to service invocation). I can assume Microservice B has already been built and deployed locally and is running in its own container (with a Dapr sidecar injected). I just need a Docker Compose file that will build Microservice A, inject a Dapr sidecar and deploy the container locally so I can test and debug.

Thanks in advance.


Solution

  • If I understand your question correctly you should try to implement the following setup:

    [ [SericeA]-[ServiceA-Dapr] ] -->[docker-network]<--[ [ServiceB]-[ServiceB-Dapr] ]
    

    On this diagram ServiceA and its Dapr sidecar joining the [docker-network]. Same applies to ServiceB and its sidecar too.

    If you create a new network:

    docker network create -d bridge my-dapr-network
    

    And then update your compose files for ServiceA and ServiceB this way:

    version: '3.4'
    
    networks:
      default:
        external:
          name: my-dapr-network
    
    services:
    
      microservicea:
        image: ${DOCKER_REGISTRY-}microservicea
        build:
          context: .
          dockerfile: MicroserviceA/Dockerfile
    
      microservicea-dapr:
        image: "daprio/daprd:edge"
        command: ["./daprd",
          "-app-id", "microservicea",
          "-app-port", "3000",
          "-placement-host-address", "placement:50005",
          "-dapr-grpc-port", "50002",
          "-components-path", "/components"]
        depends_on:
          - microservicea
        network_mode: "service:microservicea"
    

    The important bit here is this part:

    networks:
      default:
        external:
          name: my-dapr-network
    

    If you make that change to both compose files you'd be able to start\stop them independently and they will always join the same existing external bridge network. As a result they will be able to communicate.