Search code examples
dockerdocker-composedapr

How to set up dapr and docker compose with state management


Very new to dapr and docker.

I followed along the dapr getting started. The simple hello world state management example worked fine. Yes Bruce, we all know you are Batman.

So next I built the weather forecast multi-container example for .NET Core. This worked beautifully. (I named my front-end razor pages "wxui" and the back-end webapi "wxapi").

Finally, I wanted to try my hand at adding state management to the weather forecaster example. I modified the front-end Razor Pages app to store and retrieve a bit of state and added a redis container to my docker-compose file.

Things are not going well.

The wxui-dapr container is exiting with this message:

time="2021-05-20T22:47:50.3179068Z" level=fatal msg="process component statestore error: redis store: error connecting to redis at localhost:6379: dial tcp 127.0.0.1:6379: connect: connection refused" app_id=wxui instance=69254f9724b0 scope=dapr.runtime type=log ver=1.1.2

I'm going to guess that the dapr sidecar container is not mapping local port 6379 to the redis container. But I have no idea how to test or fix that.

Here's my docker-compose.yml file, if that's useful:

version: '3.4'

services:
  redis:
    image: "redis:alpine"
    ports:
      - "6379:6379"
    networks:
      - wx-hello-world

  wxui:
    image: ${DOCKER_REGISTRY-}wxui
    build:
      context: .
      dockerfile: WxUI/Dockerfile
    ports:
      - "51000:50001"
    networks:
      - wx-hello-world
    depends_on:
      - redis

  wxui-dapr:
    image: "daprio/daprd:latest"
    command: [ "./daprd", "-app-id", "wxui", "-app-port", "80", "-components-path", "/components" ]
    volumes:
      - "./components/:/components"
    depends_on:
      - wxui
    network_mode: "service:wxui"

  wxapi:
    image: ${DOCKER_REGISTRY-}wxapi
    build:
      context: .
      dockerfile: WxAPI/Dockerfile
    ports:
      - "52000:50002"
    networks:
      - wx-hello-world
  
  wxapi-dapr:
    image: "daprio/daprd:latest"
    command: [ "./daprd", "-app-id", "wxapi", "-app-port", "80" ]
    depends_on:
      - wxui
      - wxapi
    network_mode: "service:wxapi"

networks:
  wx-hello-world:

I can provide other logs or data as needed (requested).

Can anyone help me figure out what is causing the wxui-dapr container to exit, and how to fix it? Thank you!


Solution

  • So in my limited environment, using my very limited understanding of docker networking, I was able to make it work. Please feel free to offer better solutions!

    I ended up changing the docker-compose.yml file to give the redis container a hostname:

    version: '3.4'
    
    services:
      redis:
        image: "redis:alpine"
        hostname: wxstate
        ports:
          - "6379:6379"
        networks:
          - wx-hello-world
    

    and then change the dapr statestore.yaml component to use that hostname:

    apiVersion: dapr.io/v1alpha1
    kind: Component
    metadata:
      name: statestore
    spec:
      type: state.redis
      metadata:
      - name: redisHost
        value: wxstate:6379
      - name: redisPassword
        value: ""
      - name: actorStateStore
        value: "true"