Skip to content

Instantly share code, notes, and snippets.

@mbierman
Last active November 5, 2023 01:20
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbierman/6cf22430ca0c2ddb699ac8780ef281ef to your computer and use it in GitHub Desktop.
Save mbierman/6cf22430ca0c2ddb699ac8780ef281ef to your computer and use it in GitHub Desktop.
Update Docker containers
#i!/bin/bash
# version 3.1.2
# https://gist.github.com/mbierman/6cf22430ca0c2ddb699ac8780ef281ef
DOCKER=$(which docker)
wait=1
image=$1
if [ "$1" = "homebridge" ]; then
container="homebridge/homebridge:ubuntu"
elif [ "$1" = "docker-notify" ]; then
container="schlabbi/docker-notify:latest"
elif [ "$1" = "unifi" ]; then
container="jacobalberty/unifi:latest"
elif [ "$1" = "ddns" ]; then
container="
/cloudflare-ddns:latest"
else
echo -e "Container not supported\nUse \"homebridge\", \"docker-notify\", \"unifi\" "
exit
fi
echo -e " Want to update $container? \n\nPress any key to continue"
# read -n 1 -s -r -p "Press any key to continue"
read -n 1 now
if [ $(hostname) = "firewalla" ] || [ $(hostname) = "Firewalla" ] ; then
echo "Firewalla"
cd /home/pi/.firewalla/run/docker/$1
echo "Checking container..."
myvar=$( sudo $DOCKER pull $container | tee /dev/tty | grep -c "Status: Image is up to date" )
if [ $myvar = 1 ] ; then
echo "No update required."
exit
else
echo "Pulling Container"
echo "stopping $1's..."
sudo $DOCKER container stop $1 && echo "removing $1's container" && sudo $DOCKER container rm $1
sudo docker-compose up -d
sudo $DOCKER ps
read -p "Press any key to continue: " now
sudo $DOCKER image prune
fi
elif [ $(hostname) = "pigpen" ]; then
echo -e "\n\nchecking for new $container container..."
myvar=$( sudo $DOCKER pull $container | tee /dev/tty | grep -c "Status: Image is up to date" )
if [ $myvar = 1 ] ; then
echo "No update required."
exit
else
echo "Pulling container..."
fi
info=$(sudo $DOCKER image inspect $container)
echo $info
if [ "$1" = "homebridge" ]; then
S6_OVERLAY_VERSION=$(echo $info | jq -r '.[] .Config .Env []' | grep S6_OVERLAY_VERSION)
HOMEBRIDGE_PKG_VERSION=$(echo $info | jq -r '.[] .Config .Env []' | grep HOMEBRIDGE_PKG_VERSION)
args="
--name=homebridge \
--hostname=homebridge\
--env=HOMEBRIDGE_CONFIG_UI_PORT=8581 \
--env=PATH=/opt/homebridge/bin:/var/lib/homebridge/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
--env="$S6_OVERLAY_VERSION" \
--env=S6_CMD_WAIT_FOR_SERVICES_MAXTIME=0 \
--env=S6_KEEP_ENV=1 \
--env=ENABLE_AVAHI=0 \
--env=USER=root \
--env=HOMEBRIDGE_APT_PACKAGE=1 \
--env=UIX_CUSTOM_PLUGIN_PATH=/var/lib/homebridge/node_modules \
--env=HOME=/home/homebridge \
--env=npm_config_prefix=/opt/homebridge \
--env=npm_config_global_style=true \
--env=npm_config_audit=false \
--env=npm_config_fund=false \
--env=npm_config_update_notifier=false \
--env=npm_config_loglevel=error \
--env="$HOMEBRIDGE_PKG_VERSION" \
--volume=/volume1/docker/homebridge:/homebridge:rw \
--volume=/homebridge \
--network=host \
--workdir=/homebridge \
--restart=always \
--label='org.opencontainers.image.title=Homebridge in Docker' \
--label='org.opencontainers.image.authors=oznu' \
--label='org.opencontainers.image.licenses=GPL-3.0' \
--label='org.opencontainers.image.url=https://github.com/oznu/docker-homebridge' \
--label='org.opencontainers.image.description=Official Homebridge Docker Image' \
--log-driver=db \
--runtime=runc \
--detach=true -t "$container""
echo -e "args: $args\n\n"
elif [ "$1" = "docker-notify" ]; then
nodeVersion=$(echo $info | jq '.[0] .ContainerConfig .Env ' | \
sed -e 's/[,"]//g' | sed -e "s|\s*||g" | grep 'NODE_VERSION' | cut -f2 -d"=")
yarnVersion=$(echo $info | jq '.[0] .ContainerConfig .Env ' | \
sed -e 's/[,"]//g' | sed -e "s|\s*||g" | grep 'YARN_VERSION' | cut -f2 -d"=")
args="
--name=docker-notify \
--hostname=docker-notify \
--env=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
--env=NODE_VERSION="$nodeVersion" \
--env=YARN_VERSION="$yarnVersion" \
--env=TZ=America/Los_Angeles \
--volume=/volume1/docker/docker-notify/config.json:/usr/src/app/config.json:rw \
--volume=/volume1/docker/docker-notify:/usr/src/app/cache:rw \
--network=host \
--workdir=/usr/src/app \
--restart=always \
--log-driver=db --detach=true -t schlabbi/docker-notify:latest /bin/sh -c 'node index.js' "
elif [ "$1" = "unifi" ]; then
GOSU_VERSION=$(echo $info | jq '.[0] .Config .Env' | \
sed -e 's/[,"]//g' | sed -e "s|\s*||g" | grep 'GOSU_VERSION' | cut -f2 -d"=")
imageURL="--label='$(echo $info | jq -r --arg key org.opencontainers.image.url '.[].Config.Labels | "\($key)=\(.[$key])"')'"
imageMaintainer="--label='$(echo $info | jq -r --arg key maintainer '.[].Config.Labels | "\($key)=\(.[$key])"')'"
imageCreated="--label='$(echo $info | jq -r --arg key org.opencontainers.image.created '.[].Config.Labels | "\($key)=\(.[$key])"')'"
imageVersion="--label='$(echo $info | jq -r --arg key org.opencontainers.image.version '.[].Config.Labels | "\($key)=\(.[$key])"')'"
imageDescription="=label='$(echo $info | jq -r --arg key org.opencontainers.image.description '.[].Config.Labels | "\($key)=\(.[$key])"')'"
imageSource="--label='$(echo $info | jq -r --arg key org.opencontainers.image.source '.[].Config.Labels | "\($key)=\(.[$key])"')'"
imageTitle="--label='$(echo $info | jq -r --arg key org.opencontainers.image.title '.[].Config.Labels | "\($key)=\(.[$key])"')'"
imageRevision="--label='$(echo $info | jq -r --arg key org.opencontainers.image.version '.[].Config.Labels | "\($key)=\(.[$key])"')'"
imageLicenses="--label='$(echo $info | jq -r --arg key org.opencontainers.image.licenses '.[].Config.Labels | "\($key)=\(.[$key])"')'"
args="
--name=unifi \
--hostname=unifi \
--env=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
--env=BASEDIR=/usr/lib/unifi \
--env=DATADIR=/unifi/data \
--env=LOGDIR=/unifi/log \
--env=CERTDIR=/unifi/cert \
--env=RUNDIR=/var/run/unifi \
--env=ODATADIR=/var/lib/unifi \
--env=OLOGDIR=/var/log/unifi \
--env=CERTNAME=cert.pem \
--env=CERT_PRIVATE_NAME=privkey.pem \
--env=CERT_IS_CHAIN=false \
--env=GOSU_VERSION="$GOSU_VERSION" \
--env=BIND_PRIV=false \
--env=RUNAS_UID0=true \
--env=UNIFI_GID=999 \
--env=UNIFI_UID=999 \
--env=TZ=America/Los_Angeles \
--volume=/volume1/docker/UniFi:/unifi:rw \
--volume=/unifi \
--volume=/var/run/unifi \
--network=host \
--workdir=/unifI \
--restart=always \
"$imageURL" \
"$imageMaintainer" \
"$imageCreated" \
"$imageVersion" \
"$imageDescription" \
"$imageSource" \
"$imageTitle" \
"$imageRevision" \
"$imageLicenses" \
--label='org.opencontainers.image.title=unifi-docker' \
--label='org.opencontainers.image.licenses=MIT' \
--log-driver=db --runtime=runc --detach=true -t jacobalberty/unifi:latest unifi"
echo -e "args: $args\n\n"
echo -e "\n\npulling new $container container"
sudo $DOCKER pull $container
# var=$(sudo $DOCKER image inspect $container | jq '.[0] .Config .Env'
elif [ "$1" = "ddns" ]; then
BASEDIR=$(dirname "$0")
API_KEY="$(cat $BASEDIR/updatedocker.txt | grep API | cut -f2 -d "=" )"
if [ -z $API_KEY ]; then
echo -e "\nSorry, no Cloudflair API key found. Add to $BASEDIR/updatedocker.txt"
exit
fi
echo $API_KEY
args="
--name=cloudflare-ddns \
--hostname=oznu-cloudflare-ddns \
--env=SUBDOMAIN=private \
--env=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
--env=QEMU_ARCH=x86_64 \
--env=S6_KEEP_ENV=1 \
--env=API_KEY="${API_KEY}" \
--env=S6_BEHAVIOUR_IF_STAGE2_FAILS=2 \
--env=CF_API=https://api.cloudflare.com/client/v4 \
--env=RRTYPE=A \
--env='CRON=*/5 * * * *' \
--env=TZ=America/Los_Angeles \
--env=PROXIED=false \
--env=ZONE=thebiermans.net \
--network=host \
--restart=always \
--log-driver=db --runtime=runc --detach=true -t oznu/cloudflare-ddns:latest"
echo -e "args: $args\n\n"
# var=$(sudo $DOCKER image inspect $container | jq '.[0] .Config .Env'
else
echo -e "Container not supported\nUse \"homebridge\", \"docker-notify\", \"unifi\" "
exit
fi
ID=$(sudo $DOCKER ps | grep $1 | cut -f1 -d ' ')
echo -e "\n\n stopping $1..." && sudo $DOCKER stop $1
echo -e "\n\nremoving $container ($ID) container" && sudo $DOCKER container rm $1
echo -e "\n\n starting $1" && sudo docker run ${args[@]}
echo -e "\n please wait ($wait seconds) for the container to restart"
function finished () {
while [ "$ready" != "Up" ] ; do
sleep $wait
ready=$(sudo docker ps | grep $1 | grep -o 'Up')
done
}
finished $1
read -n 1 -s -r -p "Press any key to continue"
sudo docker image prune -f
sudo docker container prune -f
sudo docker system prune -f
else
echo "Not sure how to upgrade this machine."
exit
fi
@mbierman
Copy link
Author

I use this to update various docker containers on several platforms so I don't have to remember how and I don't make any mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment