Home-Assistant Docker Upgrade Script

I created a script for updating my Home-Assistant Docker. It looks to see if a current container named Home-Assistant exists, if it does, it deletes it, pulls the latest Docker image, then creates the new container.

I did a search and didn’t find this in the forum at all, so I figured I’d share.

#!/bin/bash

name=home-assistant

matchingStarted=$(docker ps --filter="name=$name" -q | xargs)
[[ -n $matchingStarted ]] && docker stop $matchingStarted

matching=$(docker ps -a --filter="name=$name" -q | xargs)
[[ -n $matching ]] && docker rm $matching

docker pull homeassistant/home-assistant:latest
docker run -d --name="home-assistant" --restart unless-stopped -v /home/homeassistant/.homeassistant:/config -v  etc/localtime:/etc/localtime:ro --privileged -v /dev/ttyACM0:/dev/ttyACM0 --net=host homeassistant/home-assistant'
2 Likes

@Kazakaz this is great. Thanks for taking the time to post this. Definitely saved me time. Just one small things there is a trailing single quote right at the end, which I don’t think should be there.

I know this is old, but this is all I found for doing this, and I wanted something a little more robust, so here’s a little update:

#!/bin/bash

# These should be overridable on the commandline!
name=homeassistant.beta
repo=ghcr.io/home-assistant/home-assistant:beta
config_dir=/home/pi/config
docker_cmd="sudo docker"

DEBUG=0
DRYRUN=0
GO=0

function usage {
        echo $(basename $0) "[--dry-run] [--debug] --do-it"
        echo "  --dry-run shows the commands that would be executed"
        echo "  --debug sets -x on the shell to show the execution"
        echo "  --do-it needs to be set to run at all!"
        exit 1
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --dry-run) DRYRUN=1 ; shift 1
      ;;

    --debug) DEBUG=1 ; set -x ; shift 1
      ;;

    --do-it) GO=1 ; shift 1
      ;;

    *) usage ;;
  esac
done

[[ "x$GO" == "x0" ]] && usage

[[ "x$DEBUG" == "x1" ]] && echo "GO = $GO | DRYRUN = $DRYRUN | DEBUG = $DEBUG"


function RunDocker {
   if [[ "x$DRYRUN" == "x1" ]] ; then
      echo $docker_cmd $*
   else
      eval $docker_cmd $* || \
         {
            exit_code=$?
            echo "Docker cmd failed! tried to run: '$docker_cmd $*' but exited with $?"
            exit $exit_code
         }
   fi
}

matchingStarted=$(RunDocker ps --filter="name=$name" -q | xargs)
if [[ -n $matchingStarted ]] ; then
   RunDocker stop $matchingStarted
fi

matching=$(RunDocker ps -a --filter="name=$name" -q | xargs)
if [[ -n $matching ]] ; then
   RunDocker rm $matching
fi

RunDocker pull $repo

RunDocker run -d \
   --name $name \
   --privileged \
   --restart=unless-stopped \
   -e TZ=MY_TIME_ZONE \
   -v $config_dir:/config \
   --network=host \
   $repo
1 Like