Codespaces meet Dapr

I recently asked a question on Twitter where Brian Clark tested their answer in Codespaces before replying.  

I thought I should have used Codespaces and set out to spend some time investigating them.  

Getting started with them is quite easy. Simply visit a GitHub repository with the code you want to work on and click on the Code button. 

Clicking on New codespace will start a development environment in your browser with the full power of Visual Studio Code!  

From the terminal, I started to see what commands I could run. I started by seeing if .NET was installed and it was. 

dotnet --version
5.0.302 

So, I thought hmm I wonder if Docker is running because if so, I can use this for Dapr development. To my delight, running docker ps worked! 

docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES 

All I had to do now was install the Dapr CLI (command-line interfaces) and this could be awesome. I opened the Dapr docs and copied a single line to install the Dapr CLI. 

wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash 

Your system is linux_amd64 
Installing Dapr CLI... 
Getting the latest Dapr CLI... 
Installing v1.3.0 Dapr CLI... 
Downloading https://github.com/dapr/cli/releases/download/v1.3.0/dapr_linux_amd64.tar.gz ... 

dapr installed into /usr/local/bin successfully. 
CLI version: 1.3.0  
Runtime version: n/a   
To get started with Dapr, please visit https://docs.dapr.io/getting-started/

A few seconds later I was ready to run dapr init. After the command was completed another docker ps and I was cooking with gas. 

docker ps 

CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS                    PORTS                                                 NAMES 
44ccc160be52   openzipkin/zipkin   "start-zipkin"           18 seconds ago   Up 16 seconds (healthy)   9410/tcp, 0.0.0.0:9411->9411/tcp, :::9411->9411/tcp   dapr_zipkin 
1b04b16daf6e   daprio/dapr:1.3.0   "./placement"            19 seconds ago   Up 17 seconds             0.0.0.0:50005->50005/tcp, :::50005->50005/tcp         dapr_placement 
2445c14280fd   redis               "docker-entrypoint.s…"   19 seconds ago   Up 17 seconds             0.0.0.0:6379->6379/tcp, :::6379->6379/tcp             dapr_redis 

 Then I thought how can I have all this happen for me? When I start a new codespace I want Dapr installed and initialized, and I want the Rest Client code extension installed.  

It was not long before I found the page describing the devcontainer.json file. With this file stored in your repository, you can configure the codespace environment. I will not cover everything you can do but I will show how I achieved my goals of having Dapr installed, initialized, and the Rest Client installed by default.  

From within the codespace I just started I created a folder named .devcontainer at the root of my repository. In that folder, I created a file called devcontainer.json. This file is an object of properties that control the behavior of your codespace.  

One property of this object is the extensions array. This array contains the extension IDs of the extensions you want to be installed.  

{
   "extensions": [ 
      "humao.rest-client" 
   ] 
} 

I saved and committed my changes to the repository. Then I deleted my current codespace and created another one from the same repository. You can also run the Codespaces rebuild container command, but it takes a lot longer and maybe overkill. 

With just that in place starting a code space from this repository will install the Rest Client extension.

Next, I wanted to install the Dapr CLI and call dapr init when the environment started.  

To accomplish this, I added a script to the .devcontainer folder named on-create.sh. From the terminal of the codespace, I ran the following command to make the script runnable.

chmod +x ./.devcontainer/on-create.sh

Finally, I added the lines below to the file.

#!/bin/bash 

echo "on-create start" >> ~/status

# install dapr cli
wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash

# initialize dapr
dapr init

echo "on-create complete" >> ~/status

To execute this script each time our codespace is created we are going to update our devcontainer.json file by adding the following lines after our extensions array.

// Use 'onCreateCommand' to run commands as part of container creation. 
"onCreateCommand": "/bin/bash -c .devcontainer/on-create.sh",

Finally, I committed and pushed my changes then recreated a codespace from the repo. A few seconds after the environment starts the on-create.sh script is run that installs the Dapr CLI and runs dapr init.

Running dapr -v shows that Dapr has been installed and initialized! 

dapr -v 
CLI version: 1.3.0  
Runtime version: 1.3.0 

Codespaces is going to make onboarding new developers incredibly easy because everything they will need will be just a click away.

Comments (1) -

  • James Murphy

    9/2/2021 6:43:24 AM | Reply

    Awesome as codespaces might be still don't have any way to find out given that I never got preview access  - I requested access pretty much on day 1 - and...

    > Codespaces access is currently limited to paid teams and organizations.

    To be clear, I don't expect it to be free, I do expect to pay a premium for a pure PAYG offering (which is what I currently need from a personal point of view), it would just be nice to have the option to try/use something that seems to have been being promoted forever

Add comment

Loading