{"id":284,"date":"2020-10-15T20:59:56","date_gmt":"2020-10-15T18:59:56","guid":{"rendered":"https:\/\/dev.drun.net\/?p=284"},"modified":"2020-10-15T20:59:56","modified_gmt":"2020-10-15T18:59:56","slug":"home-assistant-docker-installation","status":"publish","type":"post","link":"https:\/\/dev.krasi.net\/?p=284","title":{"rendered":"Home Assistant &#8211; docker installation"},"content":{"rendered":"\n<p>I will describe quickly how to set-up and prepare RPI4 for Home Assistant. We will use this as a base to install docker and configure everything.<\/p>\n\n\n\n<p>You can use the imager from <a href=\"https:\/\/www.raspberrypi.org\/downloads\/raspberry-pi-os\/\">here<\/a> , but use the <strong>lite<\/strong> image. You don&#8217;t need the desktop version.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Install Raspberry OS<\/h4>\n\n\n\n<p>Don&#8217;t forget to update and upgrade all packages:<\/p>\n\n\n\n<p><code>apt update &amp;&amp; sudo apt upgrade<\/code> , then reboot.<\/p>\n\n\n\n<p>When you first boot, you can configure the system with <\/p>\n\n\n\n<p><code>sudo raspi-config<\/code><\/p>\n\n\n\n<p>Don&#8217;t forget to start ssh, if you need remote ssh access.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Install Docker<\/h4>\n\n\n\n<p>We&#8217;ll assume you execute everything with a pi user or another who has sudo privileges<\/p>\n\n\n\n<p>Execute<\/p>\n\n\n\n<p><code>sudo apt install docker.io<\/code><\/p>\n\n\n\n<p>Don&#8217;t forget to add yourself to docker group, so you can execute docker commands without sudo:<\/p>\n\n\n\n<p><code>sudo usermod pi -a -G docker<\/code><\/p>\n\n\n\n<p>(replace pi with the user you&#8217;re using, if needed). You need to logout and login for the changes to take effect.<\/p>\n\n\n\n<p>After that, what I do is install a few scripts that will help me manage everything<\/p>\n\n\n\n<p><code>cd<\/code><br><code>mkdir docker<\/code><br><code>mkdir scripts<\/code><br><code>cd scripts<\/code><br><\/p>\n\n\n\n<p>Use your favorite editor, mine is mcedit.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Portainer ( <code>$HOME\/scripts\/run_portainer<\/code> )<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\ndocker pull portainer\/portainer-ce\n\ndocker stop portainer\ndocker rm portainer\n\ndocker run --name=portainer \\\n  --restart=unless-stopped -d \\\n  -l \"diun.enable=true\" \\\n  -v \/var\/run\/docker.sock:\/var\/run\/docker.sock \\\n  -v $HOME\/docker\/portainer:\/data \\\n  -p 9000:9000 \\\n  -p 8000:8000 \\\n  portainer\/portainer-ce<\/code><\/pre>\n\n\n\n<p>Replace <code>$HOME\/docker <\/code>with the actual location where you want to store your docker files locally ( if you don&#8217;t it will create in your home folder ). This helps next time when you&#8217;re rebuilding docker instances so you don&#8217;t lose anything.<\/p>\n\n\n\n<p>Make it executable and run it:<\/p>\n\n\n\n<p><code>chmod +x run_portainer<br>.\/run_portainer<\/code><\/p>\n\n\n\n<p>This will start portainer on port 9000. You can open it via web browser on <code>http:\/\/<strong>IP<\/strong>:9000<\/code> and set it up. It&#8217;s a great software to monitor at and manage your docker environment.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">MQTT ( <code>$HOME\/scripts\/run_mqtt<\/code> )<\/h5>\n\n\n\n<p>MQTT is really important piece of software, that will allow all our gateways and sensors to actually send data to Home Assistant. Edit <code>$HOME\/scripts\/run_mqtt<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\ndocker stop mqtt ; docker rm mqtt\n\ndocker pull eclipse-mosquitto\n\ndocker run --name mqtt -d --restart=unless-stopped \\\n  -p 1883:1883 \\\n  --net mqtt \\\n  --ip 192.168.20.10 \\\n  -l \"diun.enable=true\" \\\n  -v $HOME\/docker\/mqtt\/config:\/mqtt\/config \\\n  -v $HOME\/docker\/mqtt\/log:\/mqtt\/log \\\n  -v $HOME\/docker\/mqtt\/data\/:\/mqtt\/data\/ \\\n  eclipse-mosquitto<\/code><\/pre>\n\n\n\n<p>Of course, replace <code>$HOME\/docker<\/code> (if you need to).<\/p>\n\n\n\n<p><code>chmod +x $HOME\/scripts\/run_mqtt<\/code><\/p>\n\n\n\n<p>Do not run it yet. We need to configure user and password first.<\/p>\n\n\n\n<p><code>cd $HOME\/docker<br>mkdir -p mqtt\/config mqtt\/log mqtt\/data<br>cd mqtt\/config<\/code><\/p>\n\n\n\n<p>Then create a file, called <code>mosquitto.conf<\/code> with the following content<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>port 1883\npassword_file \/mqtt\/config\/passwd\nallow_anonymous false<\/code><\/pre>\n\n\n\n<p>Create the mqtt network. Here we use static IP address for the MQTT server, so that the other container we can configure to use that IP address. If you want, you can omit that, but it&#8217;s better to just leave it.<\/p>\n\n\n\n<p><code>docker network create -d bridge --subnet=192.168.20.0\/24 mqtt<\/code><\/p>\n\n\n\n<p>Now you can start it for the first time:<\/p>\n\n\n\n<p><code>cd $HOME\/scripts<br>.\/run_mqtt<\/code><\/p>\n\n\n\n<p>Now it&#8217;s time to generate password. Enter into the container with:<\/p>\n\n\n\n<p><code>docker exec -it mqtt \/bin\/sh<\/code><\/p>\n\n\n\n<p>Then execute<\/p>\n\n\n\n<p><code>mosquitto_passwd -c \/mqtt\/config\/passwd hass<\/code><\/p>\n\n\n\n<p>Enter the password when prompted twice. Try to use generated or hard to guess password. You can add as many users you want, just omit the <code>-c<\/code> flag next time. <\/p>\n\n\n\n<p>You can now exit the container with <code>ctrl + d<\/code>. Your MQTT server is configured. We&#8217;ll use that for Home assistant configuration.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Home assistant itself ( <code>$HOME\/scripts\/run_hass<\/code> )<\/h5>\n\n\n\n<p>You can configure several things in this shell script, but we&#8217;ll get to that. For now, use the basics<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/sh\n\ndocker pull homeassistant\/home-assistant\n\ndocker stop hass; docker rm hass\n\ndocker run -d --name=hass \\\n    --net=host \\\n    --restart=unless-stopped \\\n    -v $HOME\/docker\/hass:\/config \\\n    homeassistant\/home-assistant\n<\/code><\/pre>\n\n\n\n<p>Here we tell it to use host network, so we don&#8217;t need to explicitly map ports. This will have some other advantages later. Save it, <code>chmod +x $HOME\/scripts\/run_hass<\/code> and run it.<\/p>\n\n\n\n<p><code>cd $HOME\/scripts &amp;&amp; .\/run_hass<\/code><\/p>\n\n\n\n<p>You can now configure Home Assistant initially from <code>http:\/\/<strong>IP<\/strong>:8123\/<\/code> , replace IP with rpi&#8217;s IP address.<\/p>\n\n\n\n<p>After doing this, open <code>$HOME\/docker\/hass\/configuration.yaml<\/code> and add the following section<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mqtt:\n  broker: localhost\n  client_id: hass\n  username: hass\n  password: PASSWORD_HERE\n  discovery: true\n  discovery_prefix: homeassistant\n  birth_message:\n    topic: 'hass\/status'\n    payload: 'online'\n  will_message:\n    topic: 'hass\/status'\n    payload: 'offline'<\/code><\/pre>\n\n\n\n<p>Of course, replace <code>PASSWORD_HERE<\/code> with your MQTT password.<\/p>\n\n\n\n<p>You can now restart home assistant from within the web interface or via <code>run_hass<\/code> command.<\/p>\n\n\n\n<p>When you&#8217;re finished, you will have your first Home Assistant setup. While pretty basic, it&#8217;s already ready to be customized.<\/p>\n\n\n\n<p>We will look into this very soon \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I will describe quickly how to set-up and prepare RPI4 for Home Assistant. We will use this as a base to install docker and configure everything. You can use the imager from here , but use the lite image. You don&#8217;t need the desktop version. Install Raspberry OS Don&#8217;t forget to update and upgrade all packages: apt update &amp;&amp; sudo apt upgrade , then reboot. When you first boot, you can configure the system with sudo raspi-config Don&#8217;t forget to start ssh, if you need remote ssh access. Install Docker We&#8217;ll assume you execute everything with a pi user or another who has sudo privileges Execute sudo apt install docker.io Don&#8217;t forget to add yourself to docker group, so you can execute docker commands without sudo: sudo usermod pi -a -G docker (replace pi with the user you&#8217;re using, if needed). You need to logout and login for the changes to take effect. After that, what I do is install a few scripts that will help me manage everything cdmkdir dockermkdir scriptscd scripts Use your favorite editor, mine is mcedit. Portainer ( $HOME\/scripts\/run_portainer ) Replace $HOME\/docker with the actual location where you want to store your docker files locally ( [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5],"tags":[10,18,20,22],"class_list":["post-284","post","type-post","status-publish","format-standard","hentry","category-home-assistant","category-software","tag-docker","tag-mqtt","tag-portainer","tag-raspberry"],"_links":{"self":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/posts\/284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=284"}],"version-history":[{"count":0,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/posts\/284\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}