Automatically Updating Home Assistant Container (and other Docker Containers)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the big things that people miss about the home assistant operating supervisor is how easily it keeps everything up to date the supervisor takes care of updating your home assistant server operating system the home assistant application itself and any add-ons that you may have installed so how do you achieve the same result when you're the supervisor and you're managing your own operating system and docker environment running home assistant container well i'm going to show you that now hey home automation guy start the show i use a simple bash script that updates both my underlying ubuntu operating system and all of the docker containers that i'm running in my home automation server a bash script is a set of linux commands that are run one by one after each other you can see here that my batch script consists of nine lines of commands and it is stored in my slash op directory the same place i store all my docker container configuration files and my dockercompose.yaml file this is another one of those pretty technical videos that has a lot of commands in it that you need to type in absolutely perfectly in order to get them to work to make this easier for you i've written a blog post that goes through all of the information in this video so that you can read it at your leisure rather than trying to pause the video and write things down off the screen i've linked to the blog post in the description below i also strongly suggest that you don't just copy and paste random bash scripts like this off the internet and run them on your system without understanding exactly what they do they could easily install malware or other malicious software onto your computer and that would be bad i'm going to take you through this script in detail to show you exactly what it is doing with each step the script is broken down into two parts the first part updates the underlying linux operating system and all of the applications that are installed directly on it like docker itself these updates should improve the stability and security of your home automation server itself the second part updates all of your individual docker containers this is the part that will update your home assistant to the latest version and also update any of the other add-on containers you may have like plex pihole node-red etc these updates should improve the stability and functionality of your individual add-on docker containers now let's take a look at what each line does one by one the shebang is at the top of all bash scripts and tells the operating system what type of file it is and how to interpret the commands this command downloads the latest information about each of the packages or applications that you've got installed on the operating system this tells your computer what needs upgrading and where to get the upgraded files from this command actually does all the upgrading of the packages that have updates available it also upgrades any of the dependencies that are needed for these applications to run this command removes any of the dependencies that are no longer needed and cleans them up so that you can free up some disk space the hyphen y part at the end of each of these commands makes it say yes to any messages that may pop up as we're going to be running this script in the background we don't want it to sit there waiting for confirmation each time it runs and not actually doing any updates the next part of the script upgrades any docker containers that you are running to their latest version this will just change the directory to the slash op directory this is where my dockercompose.yaml file lives and all of the docker compose commands need to be run in the directory that this file is in this command pulls down the latest version of any of the docker images that you've specified in your docker compose yaml file as well as their dependencies this command should be familiar to you by now it recreates any containers that have new versions of their images available this deletes any old versions of docker images that are no longer being used docker images can be quite large so removing old ones can free up quite a lot of disk space the switches at the end remove all images and force it to do so without asking you for confirmation similar to the dash y switch in the apt commands above this deletes any of the storage volumes that are no longer being used by your containers once again freeing up disk space now you know what this script does you can copy it to your linux computer as i mentioned you can find the script in the companion blog post which i've linked in the description below i'm going to use vs code to get this file onto my computer if you don't know how to set up vs code to directly edit files on a remote computer using ssh then check out the video i did about this which i've also linked in the description you can also ssh into the computer directly and use a text editor like vi or nano to add these files to but i much prefer vs code once you're connected to the remote computer via ssh you can create a new file i suggest you call it update.sh copy the contents of the script into this file and then save it we'll now create a new ssh session to this computer using vs code and you'll see we're already inside the slash opt directory if you now try and run this bash script you'll probably get an error message saying file not found by default linux doesn't allow files and scripts to be executed for security reasons so you'll need to change the permissions of the file using the sudo chamod 755 command on the update.sh file if you now try to run the script again you'll see it downloading and implying updates once it's finished with the operating system updates you'll see it's starting to update your docker images and recreate any of the containers that have been updated finally it'll delete any unneeded container images and volumes so you can see we've recovered almost 2 gigabytes of storage space you can run this update anytime you want by simply sshinging into the server and running the update shell script before you update your system i strongly recommend that you back everything up in case an update breaks something if you're not sure how to back up your docker server i've made a video which i've linked in the description which shows you how to do this i don't want to log into my linux server all the time to run this update script so on my system i've created a cron job to automatically run this backup script once a week this keeps my operating system and my docker containers up to date automatically some people are reluctant to do this automatically as it may break their system without their knowledge and that is a fair stance to take but i've been running this for over a year now and i've had very few problems with it cron is a linux tool that basically just runs scripts and things automatically at a time that you specify it's very similar to task scheduler on windows and each scheduled task that you configure is called a cron job cron is stupidly complicated to understand and i'm not going to try and explain it in a lot of detail because someone has already done that in this beginner's guide to cron job article which i've linked in the description below i run my updates at 5am every saturday and i do this for two reasons firstly my backups automatically run at 2am every day which means i'm likely to have a fresh backup ready and available before this update runs in case anything goes wrong secondly home assistant updates are released on the first wednesday of each month if i wait until saturday to update my system it gives me time to read any of the breaking changes or learn if there are any issues with the update that i need to address before it's installed to set up a new cron job to automatically run your updates you need to ssh into your linux server and run the sudo crontab hyphen e command like this if this is the first time you're creating a cron job you might be asked what editor you want to use to edit the cron file i recommend choosing nano as i find it the easiest now scroll all the way down to the bottom of the file and paste this line in which again can be found in the companion blog post linked in the description this will run your update shell script from the slash op directory at the fifth hour of the sixth day of each week which basically just means saturday at 5am press ctrl x to exit nano and say yes to saving the file your new update script will now be automatically run next saturday at 5 am if you want to change the date and time that the script runs you can run the cron tab hyphen e again and change the schedule information at the front of the slash opt update.sh part of the line i suggest reading the beginner's guide to cron jobs article to understand what you should change these two in order to get your desired results now your home automation server will automatically keep itself and any of the applications that you run on it up to date with the latest security and feature updates i've been using this script for over a year now and i've never had any problems with it it's occasionally a bit weird to log into one of my home automation applications to find that it's been updated with a new set of features or user interface but that's a good problem to have i've never had these updates cause me any problems as long as i've made sure that i've read the breaking changes part of the home assistant release notes before the update is automatically applied this is the last video in my home assistant on docker video series if you've watched them all as i've released them then i really hope that you learned something useful i know i learned a ton just by making them and hearing from you all in the comments if you haven't seen them then check out this playlist which shows you all the videos in the series from start to finish if you'd like me to do a video about a particular home assistant add-on or home automation application running on docker then let me know in the comments below whilst you're down there click the subscribe button so that together we can make your home smarter
Info
Channel: Home Automation Guy
Views: 27,083
Rating: undefined out of 5
Keywords:
Id: Wx1TsuTgv_Q
Channel Id: undefined
Length: 8min 59sec (539 seconds)
Published: Wed Mar 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.