How can I check image exist on docker hub?

12/2/2019

I want to check If my image exist on docker hub or not. I find 2 solution.

1) docker pull alpine:invalid > /dev/null && echo "success" || echo "failed"

2)

# set username and password
UNAME="user"
UPASS="password"

function docker_tag_exists() {
    TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
    EXISTS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/$1/tags/?page_size=10000 | jq -r "[.results | .[] | .name == \"$2\"] | any")
    test $EXISTS = true
}

if docker_tag_exists library/nginx 1.7.5; then
    echo exist
else 
    echo not exists
fi

Solution one is bash script and solution 2 is using jq and I do not want to install jq.

Does exist a way that check existance of an image by simple curl?

like this for harbor

curl -u $USERNAME:$PASSWORD -s -o /dev/null -I -w \"%{http_code}\" -X GET \"http://${REGISTRY_HOST}/api/repositories/images%2F${repo_name}/tags/${image_tag}\"
-- yasin lachini
docker
jenkins
kubernetes

0 Answers