'How to check if a Docker image with a specific tag exist on registry?
I want to check if docker image with a specific tag exist on registry.
I saw this post: how-to-check-if-a-docker-image-with-a-specific-tag-exist-locally
But it handles images on local system.
How can I use docker image inspect
(or other commands) to check if image with specific tag exists on remote registry ?
Solution 1:[1]
I found the way without pulling:
curl -X GET http://my-registry/v2/image_name/tags/list
where:
- my-registry - registry name
- image_name - the image name I search for
The result shows all the tags in the registry
Solution 2:[2]
Another possibility is to use docker pull
- if the exit code is 1, it doesn't exist. If the exit code is 0, it does exist.
docker pull <my-registry/my-image:my-tag>
echo $? # print exit code
Disadvantage: If the image actually exists (but not locally), it will pull the whole image, even if you don't want to. Depending on what you actually want to do and achieve, this might be a solution or waste of time.
Solution 3:[3]
There is docker search
but it only works with Docker Hub. A universal solution will be a simple shell script with docker pull
:
#!/bin/bash
function check_image() {
# pull image
docker pull $1 >/dev/null 2>&1
# save exit code
exit_code=$?
if [ $exit_code = 0 ]; then
# remove pulled image
docker rmi $1 >/dev/null 2>&1
echo Image $1 exists!
else
echo "Image $1 does not exist :("
fi
}
check_image hello-world:latest
# will print 'Image hello-world:latest exists!'
check_image hello-world:nonexistent
# will print 'Image hello-world:nonexistent does not exist :('
The downsides of the above are slow speed and free space requirement to pull an image.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user3668129 |
Solution 2 | mozzbozz |
Solution 3 |