Dockerize Javascript IOT Applications
Tags: iot, docker, arduino, node
Published: 2023-07-03 21:52:23



![I am eating my breakfast while doing IOT Development in a Coffee Shop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bq9t1euwg1wtso6szrb1.jpg)

I am a [Docker Captain](https://www.docker.com/captains/ulises-gascon/) and I love build Robots and IoT devices.

In those years I have been doing a lot of IoT prototypes for companies and for myself. I have been using Arduino, Raspberry Pi, and other microcontrollers.

There are few things that make IoT development very different from other types of software development. One of them is that you need to have a physical device to test your code or build a good simulator. This means that you need to have a device that you can connect to your computer and run your code on it.

It was very common for me to have a lot of devices, sensors and wires in my backpack while commuting to work. I end up having many funny situations when I was doing IoT development in Coffee Shops around the world, just like the picture above and [many others](https://twitter.com/kom_256/status/679580650307977216/photo/1) from 2015.


## Introduction

When Arduino started to become popular, I was very excited about the idea of building my own IoT devices. I started to learn how to program in C/C++ and I bought a lot of sensors and other components to build my own projects.

But I soon realized that I was spending too much time on the Arduino source code, so I decided to use Javascript instead. I started to use [Johnny-Five](https://johnny-five.io/) and I was able to build complex IoT device in a few hours.


## Prototype is the new MVP


![Very simple IOT device with a RGB light controlled with JS in a candy jar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3wwaoi35jevkdv0s241x.jpg)


While the [JSDayES 2016](https://twitter.com/fictiziaescuela/status/726083557601497088) was taking place, I was in one of the stands. During the breaks I was able to hack the welcome candy jar and build a simple IoT device that was controlled by a Node.js server. The device was able to change the color of the light depending on the logic that you define in [a simple Node.js App](https://opensource.ulisesgascon.com/jsdayes-golosinas-iot).


## Why Johnny Five is so cool?


![Johnny Five Banner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9ua1bn77rvxhjozyg9a.png)



The library [Johnny-Five](https://johnny-five.io/) is a Javascript library that allows you to control Arduino and other microcontrollers using Javascript. It is very easy to use and it has a lot of examples that you can use to learn how to use it.

As a Javascript developer, I was very happy to be able to use the same language to build IoT devices:

The typical blink led example for an Arduino board looks like this:


```javascript
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  var led = new five.Led(13);
  led.blink(1000);
});
```

In compare to the classic approach:

```c
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}
```

It might not look like a big difference, but when you are building complex IoT devices, it makes a big difference. You can use all the Javascript features like Promises, Async/Await, etc. to build your IoT devices and all the compute power that the host device provides, as well all the modern communication that we expect today (HTTP APIS, SDKs..).

This will force you to connect (USB, Bluetooth...) the IoT device to a computer all the time as the device won't have the instructions recorded.

Obviously not all the IOT projects fits this requirements, but for a simple prototype is a fantastic solution.

## Why should I Dockerize my project?

Johnny Five is a fantastic library, but some of their dependencies have a complex installation process (under the hood), this sometimes makes difficult to install the library in some environments. Specially if you need to change between devices, it was very frustrating for me at the beginning until I decided to use Docker to make it easier to execute.

## Dockerize your JS IoT application


I created this [repository](https://github.com/UlisesGascon/POC-docker-johnny-five) to dockerize the blink example, so it is easier to follow and explore the details.

### Get the necessary software

Before you begin, you need to have [Docker installed](https://docs.docker.com/engine/install/) on your machine.


### Write a Dockerfile

Here is a hyper simplified Dockerfile, if you want to build a production ready example [Start from this](https://nodejs.org/en/docs/guides/nodejs-docker-webapp)

If you speak Spanish I also recommend you my ebook ["Docker Seguro"](https://dockerseguro.ulisesgascon.com/)

```dockerfile
FROM node:18.14.1
WORKDIR /usr/src/app
RUN apt-get update
RUN apt-get -y install udev
COPY package*.json ./
RUN npm install --unsafe-perm --build-from-source=serialport
COPY . .
CMD [ "npm", "run", "start" ]
```

### Build the Docker image

Using the `docker build` command, create a Docker image.

```bash
docker build -t blink .
```

### Run your application in a Docker container

Launch your application using the `docker run` command, specifying the necessary parameters to configure your container.

Here is the tricky part, you will need to pass the USB device to the container, so it can access the Arduino board.

```bash
docker run -it --rm --privileged --device=/dev/ttyUSB0 blink
``` 

In my case the port was `/dev/ttyUSB0`, but this might be different for you. In this [Guide](https://www.mathworks.com/help/supportpkg/arduinoio/ug/find-arduino-port-on-windows-mac-and-linux.html) you can find more information.

Sometimes access to USB devices from Docker can be tricky, especially in macOS.

> **Can I pass through a USB device to a container?**
> Unfortunately, it is not possible to pass through a USB device (or a serial port) to a container as it requires support at the hypervisor level.
> [Docker: General FAQs for Desktop](https://docs.docker.com/docker-for-mac/faqs/#can-i-pass-through-a-usb-device-to-a-container)

But there are many workarounds from the community:
- [Docker - a way to give access to a host USB or serial device?](https://stackoverflow.com/a/24231872)
- [How to use a USB device in a Docker container on Mac](https://mil.ad/blog/2018/access-usb-devices-in-container-in-mac.html)

### Publish your Docker image

Optionally, you can publish your Docker image to a container registry, such as Docker Hub, for easy access and distribution.