Upgrade portainer docker container

Script to upgrade a portainer docker container to latest version, given that it was originally installed as described in Portainer Server Deployment:

#!/bin/bash -e

VERSION=$(curl -sSL https://api.github.com/repos/portainer/portainer/releases/latest | jq -r .tag_name)

IMAGE=$(docker ps -f name=portainer  --format "{{.Image}}")
CONTAINER=$(docker ps -f name=portainer  --format "{{.ID}}")

read -rp "Upgrade portainer to version $VERSION (removing container $CONTAINER and image $IMAGE)? (y|n) [n]:" ANSWER
if [[ ${ANSWER:="n"} = "y" ]]; then

   docker stop "$CONTAINER"
   docker rm "$CONTAINER"
   docker rmi "$IMAGE"

   docker run -d -p 8000:8000 -p 9000:9000 \
      --name=portainer \
      --restart=always \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v portainer_data:/data \
   echo "aborting..."
   exit 1

download: upgrade_portainer.sh