Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env bash
set -e
IMAGE="URL"
CID=$(sudo docker ps | grep $IMAGE | awk '{print $1}')
sudo docker pull $IMAGE
for im in $CID
do
LATEST=`sudo docker inspect --format "{{.Id}}" $IMAGE`
RUNNING=`sudo docker inspect --format "{{.Image}}" $im`
NAME=`sudo docker inspect --format '{{.Name}}' $im | sed "s/\///g"`
echo "Latest:" $LATEST
echo "Running:" $RUNNING
if [ "$RUNNING" != "$LATEST" ];then
echo "upgrading $NAME"
sudo docker stop $NAME
sudo docker rmi -f $RUNNING
sudo docker run -d -p 80:80 $IMAGE
sudo docker rm $(sudo docker ps -a -q)
else
echo "$NAME up to date"
fi
done
@likejazz
Copy link
Author

likejazz commented Dec 13, 2018

The latest version remaining on the server. This version is also no longer in use.

sudo docker pull $IMAGE
for im in $CID
do
    LATEST=`sudo docker inspect --format "{{.Id}}" $IMAGE`
    RUNNING=`sudo docker inspect --format "{{.Image}}" $im`
    NAME=`sudo docker inspect --format '{{.Name}}' $im | sed "s/\///g"`
    echo "Latest:" $LATEST
    echo "Running:" $RUNNING
    if [ "$RUNNING" != "$LATEST" ];then
        echo "upgrading $NAME"
        sudo docker stop $NAME
        sudo docker rmi -f $RUNNING
        sudo docker run -d -v /home/deploy/simpson:/simpson/simpson -p 5000:5000 $IMAGE
        sudo docker rm $(sudo docker ps -a -q)
    else
        echo "$NAME up to date"
    fi
done

@likejazz
Copy link
Author

likejazz commented Mar 12, 2020

The newer version for AWS ECR & EC2 instances.

#!/bin/bash -e

#
# This script must be located within the EC2 instances.
#

if [ -z "$1" ]; then
  cat <<END
USAGE:
  update-docker.sh [AWS_OTP]
END
  exit 1
fi

readonly AWS_OTP="${1}"
readonly ECR_REGISTRY=0996xxxx.dkr.ecr.ap-northeast-2.amazonaws.com/edith/edith-chips
readonly CID=$(sudo docker ps | grep ${ECR_REGISTRY} | awk '{print $1}')
readonly VERSION=$(date '+%y.%m.%d')

echo "${AWS_OTP}" | sudo docker login --username AWS --password-stdin ${ECR_REGISTRY}
sudo docker pull ${ECR_REGISTRY}:"${VERSION}"

for im in ${CID}; do
  LATEST=$(sudo docker inspect --format "{{.Id}}" ${ECR_REGISTRY}:"${VERSION}")
  RUNNING=$(sudo docker inspect --format "{{.Image}}" $im)
  NAME=$(sudo docker inspect --format '{{.Name}}' $im | sed "s/\///g")

  echo "Latest:" "$LATEST"
  echo "Running:" "$RUNNING"

  if [ "$RUNNING" != "$LATEST" ]; then
    echo "Upgrading $NAME"
    sudo docker stop "$NAME"
    sudo docker rmi -f "$RUNNING"
    sudo docker run -d -p 80:80 ${ECR_REGISTRY}:"${VERSION}"
  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