How-To: Run Dapr in self-hosted mode with Docker

How to deploy and run Dapr in self-hosted mode using Docker

This article provides guidance on running Dapr with Docker on a Windows/Linux/macOS machine or VM.

Prerequisites

Initialize Dapr environment

To initialize the Dapr control-plane containers and create a default configuration file, run:

  1. dapr init

Run both app and sidecar as a process

The dapr run CLI command can be used to launch a Dapr sidecar along with your application:

  1. dapr run --app-id myapp --app-port 5000 -- dotnet run

This command will launch both the daprd sidecar binary and run dotnet run, launching your application.

Run app as a process and sidecar as a Docker container

Alternately, if you are running Dapr in a Docker container and your app as a process on the host machine, then you need to configure Docker to use the host network so that Dapr and the app can share a localhost network interface.

Note

The host networking driver for Docker is only supported on Linux hosts.

If you are running your Docker daemon on a Linux host, you can run the following to launch Dapr:

  1. docker run --net="host" --mount type=bind,source="$(pwd)"/components,target=/components daprio/daprd:edge ./daprd -app-id <my-app-id> -app-port <my-app-port>

Then you can run your app on the host and they should connect over the localhost network interface.

Run both app and Dapr in a single Docker container

For development purposes ONLY

It is not recommended to run both the Dapr runtime and an application inside the same container. However, it is possible to do so for local development scenarios.

In order to do this, you’ll need to write a Dockerfile that installs the Dapr runtime, Dapr CLI and your app code. You can then invoke both the Dapr runtime and your app code using the Dapr CLI.

Below is an example of a Dockerfile which achieves this:

  1. FROM python:3.7.1
  2. # Install dapr CLI
  3. RUN wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash
  4. # Install daprd
  5. ARG DAPR_BUILD_DIR
  6. COPY $DAPR_BUILD_DIR /opt/dapr
  7. ENV PATH="/opt/dapr/:${PATH}"
  8. RUN dapr init --slim
  9. # Install your app
  10. WORKDIR /app
  11. COPY python .
  12. RUN pip install requests
  13. ENTRYPOINT ["dapr"]
  14. CMD ["run", "--app-id", "nodeapp", "--app-port", "3000", "node", "app.js"]

Remember that if Dapr needs to communicate with other components i.e. Redis, these also need to be made accessible to it.

Run on a Docker network

If you have multiple instances of Dapr running in Docker containers and want them to be able to communicate with each other i.e. for service invocation, then you’ll need to create a shared Docker network and make sure those Dapr containers are attached to it.

You can create a simple Docker network using:

  1. docker network create my-dapr-network

When running your Docker containers, you can attach them to the network using:

  1. docker run --net=my-dapr-network ...

Each container will receive a unique IP on that network and be able to communicate with other containers on that network.

Run using Docker-Compose

Docker Compose can be used to define multi-container application configurations. If you wish to run multiple apps with Dapr sidecars locally without Kubernetes then it is recommended to use a Docker Compose definition (docker-compose.yml).

The syntax and tooling of Docker Compose is outside the scope of this article, however, it is recommended you refer to the official Docker documentation for further details.

In order to run your applications using Dapr and Docker Compose you’ll need to define the sidecar pattern in your docker-compose.yml. For example:

  1. version: '3'
  2. services:
  3. nodeapp:
  4. build: ./node
  5. ports:
  6. - "50001:50001" # Dapr instances communicate over gRPC so we need to expose the gRPC port
  7. depends_on:
  8. - redis
  9. - placement
  10. networks:
  11. - hello-dapr
  12. nodeapp-dapr:
  13. image: "daprio/daprd:edge"
  14. command: [
  15. "./daprd",
  16. "--app-id", "nodeapp",
  17. "--app-port", "3000",
  18. "--placement-host-address", "placement:50006", # Dapr's placement service can be reach via the docker DNS entry
  19. "--components-path", "./components"
  20. ]
  21. volumes:
  22. - "./components/:/components" # Mount our components folder for the runtime to use. The mounted location must match the --components-path argument.
  23. depends_on:
  24. - nodeapp
  25. network_mode: "service:nodeapp" # Attach the nodeapp-dapr service to the nodeapp network namespace
  26. ... # Deploy other daprized services and components (i.e. Redis)
  27. placement:
  28. image: "daprio/dapr"
  29. command: ["./placement", "--port", "50006"]
  30. ports:
  31. - "50006:50006"
  32. networks:
  33. hello-dapr: null

For those running the Docker daemon on a Linux host, you can also use network_mode: host to leverage host networking if needed.

To further learn how to run Dapr with Docker Compose, see the Docker-Compose Sample.

Run on Kubernetes

If your deployment target is Kubernetes please use Dapr’s first-class integration. Refer to the Dapr on Kubernetes docs.

Name resolution

Dapr by default uses mDNS as the name resolution component in self-hosted mode for service invocation. If you are running Dapr on virtual machines or where mDNS is not available, then you can use the HashiCorp Consul component for name resolution.

Docker images

Dapr provides a number of prebuilt Docker images for different components, you should select the relevant image for your desired binary, architecture, and tag/version.

Images

There are published Docker images for each of the Dapr components available on Docker Hub.

Tags

Linux/amd64

  • latest: The latest release version, ONLY use for development purposes.
  • edge: The latest edge build (master).
  • major.minor.patch: A release version.
  • major.minor.patch-rc.iteration: A release candidate.

Linux/arm/v7

  • latest-arm: The latest release version for ARM, ONLY use for development purposes.
  • edge-arm: The latest edge build for ARM (master).
  • major.minor.patch-arm: A release version for ARM.
  • major.minor.patch-rc.iteration-arm: A release candidate for ARM.

Last modified July 25, 2022: Update docker compose example (#2657) (1a1fae1b)