How do I check if an image:tag exists in gitlab container registry

Dineshs91 picture Dineshs91 · Dec 5, 2017 · Viewed 9.8k times · Source

I know that this can be done with dockerhub. I want to know if there is something similar available for gitlab registry.

The use case is that, I have written a fabric script to revert a deployment to a particular tag provided by the user. Before actually pulling in the images, I want to know whether an image with the specified tag exists in the registry and warn the user accordingly.

I've searched in their documentation, but couldn't find anything.

Note: User here is the person who is deploying the code.

Answer

Morty picture Morty · Aug 29, 2018

Update: I added a solution that works without access to the docker server (non-privileged mode) below.

Ok, here is a solution I came up with using the docker:stable image by enabling the experimental client features.

mkdir -p ~/.docker
"echo '{\"experimental\": \"enabled\"}' > ~/.docker/config.json"
docker  login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
docker  manifest inspect $IMGNAME:$IMGTAG > /dev/null && exit || true

The exit terminates the build script in case that tag already exists. Also you should be aware that ~/.docker/config.json is overwritten. That is also why the login must happen afterwards.

Update: Instead of writing to the config one can also set the DOCKER_CLI_EXPERIMENTAL environment variable to enabled. Thus the first two lines can be replaced with export DOCKER_CLI_EXPERIMENTAL=enabled

Update: If you don't have privileged mode turned on and thus no access to the docker-daemon, you can use the registry-api scripts provided by harbor ( Note that they are python2.). This comes handy if you are building a docker image using kaniko, where no access to the docker-daemon is needed.