Skip to content

Instantly share code, notes, and snippets.

@cmavr8
Last active May 29, 2020 08:53
Show Gist options
  • Save cmavr8/4346368fca700d75356e to your computer and use it in GitHub Desktop.
Save cmavr8/4346368fca700d75356e to your computer and use it in GitHub Desktop.
Pulls a Docker image and compares it to running ones. Stops/removes/restarts the ones that are outdated. Warning: This is obviously a few years old and has not been updated. Do not use unless you review and ensure it's still OK. BTW, there are better ways to do this today.
#!/usr/bin/env bash
# Pulls a Docker image and compares it to running ones. Stops/removes/restarts the ones that are outdated.
# Put in /etc/cron.daily and make executable. One per image.
# From: http://stackoverflow.com/questions/26423515/how-to-automatically-update-your-docker-containers-if-base-images-are-updated/26548914
set -e
IMAGE="<DEVELOPER>/<DOCKERIMAGE>:latest"
CID=$(docker ps | grep $IMAGE | awk '{print $1}')
docker pull $IMAGE
for im in $CID
do
LATEST=`docker inspect --format "{{.Id}}" $IMAGE`
RUNNING=`docker inspect --format "{{.Image}}" $im`
NAME=`docker inspect --format '{{.Name}}' $im | sed "s/\///g"`
echo "Latest:" $LATEST
echo "Running:" $RUNNING
if [ "$RUNNING" != "$LATEST" ];then
echo "upgrading $NAME"
docker stop $NAME
docker rm -f $NAME
docker run <WHATEVER RUN CONFIGURATION YOU ARE NORMALLY USING>
else
echo "$NAME up to date"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment