Developing Dapr applications with Dev Containers

How to setup a containerized development environment with Dapr

The Visual Studio Code Dev Containers extension lets you use a self-contained Docker container as a complete development environment, without installing any additional packages, libraries, or utilities in your local filesystem.

Dapr has pre-built Dev Containers for C# and JavaScript/TypeScript; you can pick the one of your choice for a ready made environment. Note these pre-built containers automatically update to the latest Dapr release.

We also publish a Dev Container feature that installs the Dapr CLI inside any Dev Container.

Setup the development environment

Prerequisites

Add the Dapr CLI using a Dev Container feature

You can install the Dapr CLI inside any Dev Container using Dev Container features.

To do that, edit your devcontainer.json and add two objects in the "features" section:

  1. "features": {
  2. // Install the Dapr CLI
  3. "ghcr.io/dapr/cli/dapr-cli:0": {},
  4. // Enable Docker (via Docker-in-Docker)
  5. "ghcr.io/devcontainers/features/docker-in-docker:2": {},
  6. // Alternatively, use Docker-outside-of-Docker (uses Docker in the host)
  7. //"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {},
  8. }

After saving the JSON file and (re-)building the container that hosts your development environment, you will have the Dapr CLI (and Docker) available, and can install Dapr by running this command in the container:

  1. dapr init

Example: create a Java Dev Container for Dapr

This is an example of creating a Dev Container for creating Java apps that use Dapr, based on the official Java 17 Dev Container image.

Place this in a file called .devcontainer/devcontainer.json in your project:

  1. // For format details, see https://aka.ms/devcontainer.json. For config options, see the
  2. // README at: https://github.com/devcontainers/templates/tree/main/src/java
  3. {
  4. "name": "Java",
  5. // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
  6. "image": "mcr.microsoft.com/devcontainers/java:0-17",
  7. "features": {
  8. "ghcr.io/devcontainers/features/java:1": {
  9. "version": "none",
  10. "installMaven": "false",
  11. "installGradle": "false"
  12. },
  13. // Install the Dapr CLI
  14. "ghcr.io/dapr/cli/dapr-cli:0": {},
  15. // Enable Docker (via Docker-in-Docker)
  16. "ghcr.io/devcontainers/features/docker-in-docker:2": {},
  17. // Alternatively, use Docker-outside-of-Docker (uses Docker in the host)
  18. //"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {},
  19. }
  20. // Use 'forwardPorts' to make a list of ports inside the container available locally.
  21. // "forwardPorts": [],
  22. // Use 'postCreateCommand' to run commands after the container is created.
  23. // "postCreateCommand": "java -version",
  24. // Configure tool-specific properties.
  25. // "customizations": {},
  26. // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
  27. // "remoteUser": "root"
  28. }

Then, using the VS Code command palette (CTRL + SHIFT + P or CMD + SHIFT + P on Mac), select Dev Containers: Rebuild and Reopen in Container.

Use a pre-built Dev Container (C# and JavaScript/TypeScript)

  1. Open your application workspace in VS Code
  2. In the command command palette (CTRL + SHIFT + P or CMD + SHIFT + P on Mac) type and select Dev Containers: Add Development Container Configuration Files...
    Screenshot of adding a remote container
  3. Type dapr to filter the list to available Dapr remote containers and choose the language container that matches your application. Note you may need to select Show All Definitions...
    Screenshot of adding a Dapr container
  4. Follow the prompts to reopen your workspace in the container.
    Screenshot of reopening an application in the dev container

Example

Watch this video on how to use the Dapr Dev Containers with your application.

Last modified March 21, 2024: Merge pull request #4082 from newbe36524/v1.13 (f4b0938)