« Back to home

Pimatic using Docker on a Raspberry Pi

I'm a great fan of using a Raspberry Pi with pimatic as the heart of my home automation. A really simple hardware platform and a great framework, with a lot of extensions make a good combination.

Every now and then I do some experimentation just to discover what the possibilities are. Usually this results in something good. But sometimes I make a mess of it. Unfortunately the pimatic intstallation including configuration and plugins can take some effort and, especially on a Pi 1, some time to install. Docker with a preconfigured pimatic image to the rescue!

Install raspbian

Skip this If you know how or already have raspbian running

  1. Flash the latest raspian image to SD card
  2. Add a file without content called "ssh" to enable ssh on boot
  3. Login using ssh
  4. sudo nano /etc/dhcpcd.conf and add:
    interface eth0 static ip_address=192.168.47.31/24 static routers=192.168.47.1 static domain_name_servers=8.8.8.8
  5. Reboot
  6. Login again
  7. sudo apt-get update
  8. sudo apt-get -u dist-upgrade

Install docker

Let the docker install script do it's magic: sudo curl -sSL https://get.docker.com | sh

Docker-compose is required to properly run docker containers
sudo apt-get -y install python-pip then run sudo pip install docker-compose

To enable docker for the pi user type
sudo usermod -aG docker pi followed by sudo service docker restart

After this log out and log in again.

Now you can run docker whitout having to type sudo in front of it each time

Run a basic pimatic image on a Pi, or Pi Zero

Type docker run -t -i -p 80:80 joshendriks/raspberry-pi-pimatic

On a Pi 2 use joshendriks/raspberry-pi2-pimatic. On a Pi 3 use joshendriks/raspberry-pi3-pimatic.

When the container is started type pimatic.js at the command prompt. Pimatic should start and can be accessed on port 80 of the Pi. (user admin, password pimatic)

When this all works we start creating our own image including your own pimatic configuration.

Build a specific image

Create a docker image is pretty straight forward. On the docker host (your Pi), create a Dockerfile by typing:
nano Dockerfile.pimaticnode

The image needs the following

  • automatic startup of pimatic
  • pimatic-homeduino plugin
  • pimatic-mqtt plugin
  • my own pymatic config file

To make that happen, add the following to your Dockerfile:

    FROM joshendriks/raspberry-pi-pimatic:4
    RUN cd /home/pimatic-app && npm install pimatic-homeduino pimatic-mqtt
    COPY pimatic_confg.json /home/pimatic-app/config.json
    ENTRYPOINT ["pimatic.js"]

For the above to work your will also need to create a pimatic_confg.json file. Put it in the same location as Dockerfile.pimaticnode

Both files are available on https://github.com/joshendriks/blog-docker-pimatic as well.

Build the docker image: docker build -t pimaticnode -f Dockerfile.pimaticnode .

Run the docker image: docker run -p 80:80 --device=/dev/ttyUSB0 -t -i pimaticnode

Note that --device=/dev/ttyUSB0 has been added so that the homeduino plugin can access the physical USB port

Again Pimatic should start and can be accessed on port 80 of the Pi. (user admin, password pimatic)

Start the image on boot

Create a docker-compose file:
nano pimaticnode.yml

Add the following content:

registry:  
  restart: always
  image: pimaticnode
  ports:
    - 0.0.0.0:80:80
  devices:
    - "/dev/ttyUSB0:/dev/ttyUSB0"

Above is the equivalent of docker run command that we used before.

Again note that /dev/ttyUSB0 has been added so that the homeduino plugin can access the physical USB port. If you do not have something connected to /dev/ttyUSB0, leave the last two lines out.

Finally run docker-compose file:
sudo docker-compose -f pimaticnode.yml up -d

This will start the container in the background. Because restart: always is in the .yml file it will also start the container upon reboot.

That's it, pimatic is now running using docker on a pi!

In my next blog I will dicuss how to have config.json outside the image and how you can store your own images.