Skip to content

Instantly share code, notes, and snippets.

@k4kratik
Last active May 22, 2021 09:27
Show Gist options
  • Save k4kratik/b5d81a60b60c00866890ad660df8b516 to your computer and use it in GitHub Desktop.
Save k4kratik/b5d81a60b60c00866890ad660df8b516 to your computer and use it in GitHub Desktop.
Script to find if your Docker image with the specific tag exists or not. Check the blog at https://blog.virtualk.xyz/check-docker-image.
function DockerImageCheckFunction() {
# you can comment all the DEBUG echo statements, there are here just for info.
DHUB_TOKEN=$(curl -sSLd "username=${DOCKER_HUB_USERNAME}&password=${DOCKER_HUB_PASSWORD}" https://hub.docker.com/v2/users/login | jq -r ".token")
echo "[DEBUG] [$(date)] Token is: $DHUB_TOKEN"
echo
echo "[DEBUG] [$(date)] Hitting the endpoint: https://hub.docker.com/v2/repositories/${DOCKER_REPO}/tags/${DOCKER_TAG}/"
REPO_RESPONSE=$(curl -sH "Authorization: JWT $DHUB_TOKEN" "https://hub.docker.com/v2/repositories/${DOCKER_REPO}/tags/${DOCKER_TAG}/")
echo
echo "[DEBUG] [$(date)] Response from Docker Hub:"
echo $REPO_RESPONSE
echo
TAG_STATUS=$(echo $REPO_RESPONSE | jq .tag_status | tr -d '"')
echo "[DEBUG] [$(date)] Tag status is: $TAG_STATUS"
echo
if [[ $TAG_STATUS == "active" ]]; then
echo Docker Image $DOCKER_REPO exists with Tag $DOCKER_TAG
else
echo "Docker Image Does Not Exist"
fi
}
# Edit The Below Varibales
DOCKER_HUB_USERNAME="myserName"
DOCKER_HUB_PASSWORD="myDockerHubToken"
DOCKER_REPO="myRepo"
DOCKER_TAG="latest"
# Calling the Function
DockerImageCheckFunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment