Skip to content

Instantly share code, notes, and snippets.

@hortonew
Last active March 3, 2023 03:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hortonew/5d312ffcc666c13616707ded5ebca73f to your computer and use it in GitHub Desktop.
Save hortonew/5d312ffcc666c13616707ded5ebca73f to your computer and use it in GitHub Desktop.
Home Assistant: Docker container update script
#!/bin/bash
# Note: This config renews letsencrypt certs for "mydomain.com". Modify this for your own domain or remove those steps.
set -e
# Update image
docker pull homeassistant/home-assistant:latest
d=`date +%Y-%m-%d-%H`
# Identify and stop old instance of home assistant
olddockerid=`docker ps -a | grep home-assistant | awk '{ print $1 }'`
echo "Stopping old home assistant docker, id: $olddockerid"
docker stop "$olddockerid"
docker container rename "$olddockerid" "home-assistant-old"
# Try to renew letsencrypt certs and copy over the fullchain/private key files
# Certs created with: certbot certonly --manual --manual-auth-hook /etc/letsencrypt/acme-dns-auth.py --preferred-challenges dns --debug-challenges -d \*.mydomain.com -d mydomain.com
certbot renew
cp /etc/letsencrypt/live/mydomain.com/fullchain.pem /opt/configs/homeassistant/certbot/fullchain.pem
cp /etc/letsencrypt/live/mydomain.com/privkey.pem /opt/configs/homeassistant/certbot/privkey.pem
# Start new instance using existing configuration: --device /dev/ttyACM0 is a zwave usb stick, remove if unnecessary
docker run --init --restart always -d --name="home-assistant-$d" -v /opt/configs/homeassistant:/config -v /etc/localtime:/etc/localtime:ro --net=host --device /dev/ttyACM0 homeassistant/home-assistant
# Identify new docker image
newdockerid=`docker ps -a | grep "home-assistant-$d" | awk '{ print $1 }'`
echo "Spawned new home assistant docker, id: $newdockerid"
# Cleanup old instance
docker rm "$olddockerid"
echo "Removed old home assistant docker, id: $olddockerid"
echo "Status:"
docker ps -a | grep homeassistant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment