How to run FHEM as a Docker Container on a Raspberry Pi

created May 31, 2017, last updated June 2, 2017.

.
closeThis post was last updated 6 years 9 months 22 days ago, some of the information contained here may no longer be actual and any referenced software versions may have been updated!

I use FHEM as the engine for home automation tasks on my Raspberry Pi. This is how to build and run FHEM as a container in Docker on your Raspberry Pi Zero, Pi2 or Pi3.

fhem docker container installation

Install Docker

Installing Docker is a no brainer

curl -sSL get.docker.com | sh
sudo usermod your-username -aG docker
reboot

Install docker-compose

Docker-compose makes building, starting and stopping Docker containers really simple.

apt-get -y install python-pip
pip install docker-compose

Build the container

Pull the container build files from github and run docker-compose build to build the container image.

git clone https://github.com/gaiterjones/docker-rpi-fhem
cd docker-rpi-fhem
docker-compose build

Start the container

docker-compose up -d

Connect to FHEM

Enter the following url into your browser to connect to FHEM.

http://my.pi:8803/fhem

Update FHEM by entering with the update command.
Restart FHEM by entering the shutdown restart command.

FHEM is now running and up to date.

You can start configuring it or copy your existing FHEM config or editing the fhem.cfg file in ./fhem. This is a Docker volume on the host mapped to /opt/fhem in the container. Docker host volumes will persist when you bring down the container (docker-compose down) or clear the volume caches (docker-compose down -v) so you will not lose your configuration or logs when the container restarts or is rebuilt.

Customising

Take a look at docker-compose.yml

# RPI
# FHEM CONTAINER
#
version: "3"

services:
    server:
        build: ./build/
        hostname: fhem
        domainname: home.com
        privileged: true
        ports:
          - "8083:8083"
          - "7072:7072"
        expose:
            - 8083
            - 7072
        volumes:
            - ./fhem:/opt/fhem
            #- /dev/ttyUSB0:/dev/ttyUSB0
        networks:
            - server
        restart: always
        healthcheck:
           test: ["CMD-SHELL", "echo 'QUIT' | nc -w 5 localhost 7072 > /dev/null 2>&1 && echo 'FHEM OK'"]
           interval: 15m
           timeout: 10s
           retries: 3
networks:
    server:

You can see we are configuring global ports 8083 and 7072 on the host as well as exposing them on the container. I have an SSL certificate installed on my Pi and use Nginx as a frontend proxy to my Docker containers, this lets me connect to FHEM using SSL

https://my.pi/fhem

To remove the global host ports delete or comment out the ports config leaving just the expose config. Nginx can be configured to proxy to fhem using the following configuration:

# FHEM
location /fhem {
  proxy_pass http://fhem_container_1:8083/fhem;
}

The container runs in privileged mode which gives it access to the hosts hardware and devices. If you are using external USB devices connected to the Pi you must add them as a volume in the container i.e.

– /dev/ttyUSB0:/dev/ttyUSB0

Make sure the fhem user running in the container has the same user id as your host user, in this case uid 1001.

	 	 
user@raspberrypi:/dev# id your-user-name	 	 
uid=1001(your-user-name) gid=1001(your-user-name) groups=1001(your-user-name),27(sudo),996(docker)	 	 

This will ensure there are no permission problems when the container tries to access devices on the host.

Comments

  1. Nick says:

    Thanks for taking the time to do this. I have got fhem running in a docker container (new to me) for the first time today 🙂

This site uses Akismet to reduce spam. Learn how your comment data is processed.