Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (2)

View Page History

h3. Automatic Docker image upgrade using crontab and bash script
See https://github.com/Cantara/devops/tree/master/pull_deploy/linux


* Newly created EC2 instance.
* {{sudo su - configservice}}
* Crontab: {{* 3 * * * /home/configservice/update_configservice.sh >> /var/log/update_configservice_cron.log 2>&1}}
* update_configservice.sh:

{code:language=bash|title=check_and_update_docker_image.sh|linenumbers=true}
set -e
date # for logging
BASE_IMAGE="some_docker_image"
REGISTRY="some_registry"
IMAGE="$REGISTRY/$BASE_IMAGE"

CONFIG_FILE_NAME=config_override.properties
CONFIG_FILE="$(pwd)/$CONFIG_FILE_NAME"
PORT_MAPPING="80:8082"

DOCKER_RUN_COMMAND="docker run -d -p $PORT_MAPPING --name $BASE_IMAGE --restart=always -e \"PHARMACYINVENTORY_VERSION=$VERSION\" -v $CONFIG_FILE:/home/pharmacyinventory/config_override.properties $IMAGE:latest"

CID=$(docker ps | grep $IMAGE | awk '{print $1}')
docker pull $IMAGE

if [ $CID ]; then # if already running
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
echo "Running DOCKER_RUN_COMMAND"
$DOCKER_RUN_COMMAND
else
echo "$NAME up to date"
fi
done
else
echo "Running DOCKER_RUN_COMMAND"
$DOCKER_RUN_COMMAND
fi
{code}

* The docker instance starts supervisord, which makes sure cron is running periodically. The crontask gets the SHA checksum for the latest version on the relevant remote repository, compares this to the running version's checksum, and if they differ downloads and installs the newest version: https://github.com/Cantara/ConfigService/tree/master/docker
** See Dockerfile -> supervisord.conf -> run_service.sh -> update_service.sh

* Jenkins Github plugin for webhooks, so we get much faster time to deploy, about 2 minutes.