Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if image:tag combination already exists on docker hub

As part of a bash script, I want to check if a particularly docker image:tag combination exists on docker hub. Also, it will be a private repository.

i.e. the pseudocode would be like:

tag = something if image:tag already exists on docker hub:     Do nothing else     Build and push docker image with that tag 
like image 845
rgareth Avatar asked Aug 20 '15 08:08

rgareth


People also ask

Can multiple docker images have the same tag?

Yes you can. As long as the images' names are different you can tag them with the same ":tag".

Does docker tag overwrite?

Currently, if you deploy an image with a user, then try to push the image again (to overwrite it) with no delete/overwrite permissions, the docker client returns a successful message. If you try to overwrite the image/tag with a different image, then there will be an error.


1 Answers

Update: Docker-Free solution see below

Using Docker

This is the solution I use with gitlab using the docker:stable image.

Login

docker login -u $USER -p $PASSWORD $REGISTRY 

Check whether it's there:

docker manifest inspect $IMGNAME:$IMGTAG > /dev/null ; echo $? 

docker will return 0 on success or 1 on failure.

If you get a warning: Update Docker or enable experimental client-features:

Set the environment variable DOCKER_CLI_EXPERIMENTAL to enabled (See Matěj's answer below)

Alternatively adjust the config (original answer):

echo '{"experimental": "enabled"}' > ~/.docker/config.json 

This will also overwrite your config. If that is not an option you need to do that manually or use jq, sed or whatever you have available.

Update: If you don't have access to a docker-daemon, e.g. because you are building a docker image using kaniko within a docker, you can use the registry-api scripts provided by harbor. Note that they are python2.

like image 57
Morty Avatar answered Sep 21 '22 01:09

Morty