Skip to content

Instantly share code, notes, and snippets.

@vermotr
Created April 28, 2016 11:24
Show Gist options
  • Save vermotr/f9ba7dc5cd7834b9c9730e4e1a5e9cab to your computer and use it in GitHub Desktop.
Save vermotr/f9ba7dc5cd7834b9c9730e4e1a5e9cab to your computer and use it in GitHub Desktop.
Bash script to upgrade docker container after its image changed
#!/usr/bin/env bash
if [ $# -lt 1 ];then
echo Usage: `basename $0` image 1>&2
exit 1
fi
set -e
IMAGE=$1
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 [ "$LATEST" != "$RUNNING" ];then
echo "upgrading $NAME"
docker stop $NAME
docker rm -f $NAME
# here, create your image
docker start $NAME
else
echo "$NAME up to date"
fi
done
@EzequielBruni
Copy link

Thank you! I may well be using this soon.

@BassT23
Copy link

BassT23 commented Feb 15, 2023

Error: failed to start containers :(

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