Recently, both Docker and Moby engine support SSH connection, which means you can connect a remote Docker engine more easily than before. In Azure IoT Edge for VS Code 1.9.0, we introduced a new extension setting(azure-iot-edge.executor.env) to inject environment variables into VS Code terminals. By leveraging the new setting and the new SSH connection feature, you can easily complete the tasks below now.
- Build IoT Edge module images on a remote device.
- Debug running modules on a remote device.
Since we can rely on the container engine on the remote machine, we can now run VS Code on our amd64 development machine and build images with an arm device. Iād like to use Raspberry Pi as my IoT Edge device and my macOS as the development environment. I will show how easy to build and debug arm32 module images with the new feature in VS Code. Letās see how it works.
Prerequisites
Setup your Raspberry Pi
- Install Raspbian on your Raspberry Pi by following this tutorial.
- Setup your Raspberry Pi as an IoT Edge device – Follow the instructions to install Azure IoT Edge runtime on Linux(arm32v7/armhf).
- Passwordless SSH access to your Raspberry Pi. (Due to this issue, till Feb 2019, you cannot use username/password for your Docker SSH connection)
- To configure a Pi with TCP-enabled Docker.
- Edit /lib/systemd/system/docker.service and add -H tcp://0.0.0.0:2375 at āExecStartā
- run sudo systemctl daemon-reload
- run sudo systemctl restart docker
- To configure a Pi with TCP-enabled Docker.
- Manage Docker as a non-root user on your Raspberry Pi, so that you donāt need to preface the docker command with sudo.
Setup your C# module development environment on MacOS
- VS Code and necessary extensions including Azure IoT Tools, C# extension.
- .NET Core 2.1 SDK
- Docker Community Edition 2.0.0.0-mac78 2018-11-19 or above (which contains the SSH support)
- If you are running macOS Sierra (and above), update your ssh config file so that you wonāt be asked to input SSH passphrase in VS Code terminal each time when trigger docker command.
Create a new project and configure the extension setting
- Open VS Code user setting, enable environment variables for the command executor and Azure IoT Edge terminals by adding the snippet below. In my case, my Piās IP is 192.168.86.74, you need change that with your IP or you can also use hostname.
"azure-iot-edge.executor.env": { "DOCKER_HOST": "ssh://pi@192.168.86.74" }
If you have tcp-enabled Docker, you also do
"azure-iot-edge.executor.env": { "DOCKER_HOST": "tcp://{YourIP}:2375" }
- Reload VS Code window. Type and run Reload Window in Command Palette.
- Create a new Solution with a C# module. (follow the instructions here in case you forgot how to do that) If you want to use your existing C# project, you might need to manually add the Dockerfile.arm32v7.debug, and update the module.json. You can find Dockerfile here on Github.
- We will soon use Raspberry Pi to build module images, switch your IoT Edge Default Platform to arm32v7 in the status bar.
Build and run IoT Edge modules on Raspberry Pi
Update both deployment.template.json and deployment.debug.template.json file due to the stability issues on resource constrained devices. Right-click the deployment.template.json file, and select Build and Push IoT Edge Solution. In VS Code terminal, the docker build command will be triggered, and this time, the build context will be sent to my Docker daemon on Raspberry Pi.
After successful build, I use the generated deployment.arm32v7.json to create a deployment to my Raspberry Pi.
After a while, the C# arm32v7 module starts running on the Raspberry Pi.
Debug the modules running on Raspberry Pi
To attach the process in the running container, VSDBG is required for each C# module image, and .netcore app is also need to be built with Debug flag. Right-click the deployment.debug.template.json file, click Build and Push IoT Edge Solution. We can get the debug version of module image with Dockerfile.arm32v7.debug.
Debug the module with IoT Edge runtime
Since I already have IoT Edge security daemon running on my Raspberry Pi, I might just want to debug my module against the IoT Edge runtime with my modules running.
- Create a deployment with deployment.debug.arm32v7.json, after a while, the debug version of SampleModule starts running.
- Update the pipeArgs in “SampleModule Remote Debug (.NET Core)” in Launch.json. Add “-H <yourSSHconnection>” in PipeArgs, so that VS Code can pick the remote process with remote Docker engine.
- Add a breakpoint in Program.cs. Navigate to Debug view, select SampleModule Remote Debug (.NET Core), and press F5. Select the remote dotnet process from the dropdown. It might take a while to hit the breakpoint due to the compute power of Pi and the network between development machine and Pi.
Debug the module with IoT Edge simulator
Iotedgehubdev can also run IoT Edge solution via SSH on Raspberry Pi The tool will start an edgeHubDev, and we can directly run the entire IoT Edge app without doing deployment during development stage.
- To avoid port conflicts with edgeHub, I might choose to stop IoT Edge runtime by running sudo systemctl stop iotedge.
- Iotedgehubdev tool requires docker-compose to run IoT Edge app from deployment template file. However, the latest stable version of docker-compose doesnāt have the support for connecting to the Docker Engine using the ssh protocol. To work around this for now, I manually installed 1.24.0-rc1 version of docker-compose on my macOS, which exactly contains ssh support.
- Right-click deployment.template.json, select Build and Run IoT Edge Solution in Simulator. After a few seconds, you can see all module starts running with the logs as in the screenshot below.
- Navigate to debug view. Navigate to Debug view, select SampleModule Remote Debug (.NET Core), and press F5. Select the remote dotnet process from the dropdown. Breakpoint should be hit.
- Stop and debug session and stop the simulator. In Comment Palette, type and run Azure IoT Edge: Stop IoT Edge Simulator.
Summary
I demonstrated how to use my macOS as the development machine to build, run and debug arm32v7 IoT Edge module images. The steps are similar if you are using Windows, but you might need to spend more time to set up SSH stuff on Windows. This article wonāt cover that part. You can also leverage Docker-machineĀ to bridge your docker-cli to remote Docker engine.
If you want to cut down the SSH connection between your VS Code and your remote Docker engine on your remote device, remember to clean up the two items below.
- azure-iot-edge.executor.env in extension settings.
- Extra pipeArgs for SSH connections in Launch.json
Azure IoT Edge for VS Code project is open-sourced on Github. If you have any feature request or encounter any issues during your daily usage, donāt hesitate to create issue on our Github repository. We are all ears.
What is the best way to test this quickly to gain trust in solution with real IoT devices & Raspberry-Pi?
Preferably, using Zigbee (Conbee) or Bluetooth or WiFi devices.
No coding to start with. Just integration to test it out…