Skip to content

Instantly share code, notes, and snippets.

@orrisroot
Created November 15, 2021 09:40
Show Gist options
  • Save orrisroot/9a6de1bd6c2db3522c05525f09ea35b6 to your computer and use it in GitHub Desktop.
Save orrisroot/9a6de1bd6c2db3522c05525f09ea35b6 to your computer and use it in GitHub Desktop.
Script to update docker images and to restart docker composed services, if used docker images has been updated.
#!/bin/bash
for cmd in docker jq; do
if ! command -v ${cmd} > /dev/null; then
echo "Error: ${cmd} is required." 1>&2
exit 1
fi
done
COMPOSE_FILE="$1"
if [ -z "${COMPOSE_FILE}" ]; then
COMPOSE_FILE="./docker-compose.yml"
fi
if [ ! -f "${COMPOSE_FILE}" ]; then
echo "Error: docker-compose.yml not found." 1>&2
exit 1
fi
IMAGES=$(docker compose -f ${COMPOSE_FILE} images -q)
if [ $? -ne 0 ]; then
echo "Error: docker compose plugin not found." 1>&2
exit 1
fi
RESTART=0
for i in $IMAGES; do
JSON=$(docker image inspect ${i})
TAG=$(echo $JSON | jq -r ".[].RepoTags[0]")
DIGEST=$(echo $JSON | jq -r ".[].RepoDigests[0]")
docker pull $TAG > /dev/null 2>&1
DIGEST_=$(docker image inspect ${TAG} | jq -r ".[].RepoDigests[0]")
if [ ${DIGEST} != ${DIGEST_} ]; then
RESTART=1
fi
done
if [ $RESTART -ne 0 ]; then
docker compose -f ${COMPOSE_FILE} down > /dev/null 2>&1
docker compose -f ${COMPOSE_FILE} up -d > /dev/null 2>&1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment