Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 286
Dineshs91 Avatar asked Dec 05 '17 19:12

Dineshs91


People also ask

How do I view image tags in docker?

There's a way to check all version tags on Docker Hub (for example), against the local docker image's “Image ID”. You can get every tag from a Docker Registry (like Docker Hub), then use every tag you found, to get the image ID information from the manifest of every image.

How do I check if an image is available in docker?

The easiest way to list Docker images is to use the “docker images” with no arguments. When using this command, you will be presented with the complete list of Docker images on your system. Alternatively, you can use the “docker image” command with the “ls” argument.

Does docker registry contain collection of images?

A registry is a storage and content delivery system, holding named Docker images, available in different tagged versions. Example: the image distribution/registry , with tags 2.0 and 2.1 . Users interact with a registry by using docker push and pull commands.

Where are docker images stored in registry?

This is stored within /var/lib/docker on the docker host, probably under a container or overlay sub directory, and then under a unique directory name per container.


3 Answers

Gitlab API can be used.

tag=tag_name
image=image_name
private_token=gitlab_private_token
project=project_number
repo_id=$(curl --header "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$project/registry/repositories" | jq -c --arg regex ".*\\$image$" '.[] | select(.path | test($regex))'.id)

if [ $( curl --header "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$project/registry/repositories/$repo_id/tags/$tag" | jq -r '.name' ) == "$tag" ] ; then
  echo "$tag exists"
else
  echo "$tag does not exist"
fi
like image 71
fparaggio Avatar answered Oct 04 '22 04:10

fparaggio


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.

like image 43
Morty Avatar answered Oct 02 '22 04:10

Morty


Unless the GitLab Container Registry supports the kind of curl dockerhub does (with v1/repositories/$1/tags/$2), I doubt it offers that feature.

For instance, issue 26866 "GitLab Registry available images list/search" is still open after 10 months.

Update for GitLab 12.2 (April 2019, 18 months later)

After working through the implementation, it made sense to create two endpoints:

  • GET /groups/:id/registry/repositories - Returns a list of all Docker container repositories for all projects within the group, similar to GET /projects/:id/registry/repositories

and

  • GET /groups/:id/registry/repositories/tags - Returns a list of all Docker container repositories for all projects within the group including all tags for each container repository. The response will look something like this:

So that could help checking if an image:tag exists.


Update GitLab 13.0 (May 2020)

Use search to quickly find and discover images hosted in the GitLab Container Registry

When you or someone on your team publishes an image to the GitLab Container Registry, you need a way to quickly find it and ensure the image was built properly.
If you’re using GitLab CI/CD to publish images with each build, it’s been very difficult to find an image efficiently within the current user interface. Instead, you’ve relied on the command line or the API.

We are excited to announce that in 13.0, we’ve added search functionality to the GitLab Container Registry.

Simply navigate to your project or group’s registry and enter an image name to see a list of all your images.

https://docs.gitlab.com/ee/user/packages/container_registry/img/container_registry_repositories_with_quickstart_v13_0.png

See documentation and issue.


See also GitLab 14.7 (January 2022)

Sort Docker tags in the Container Registry browser

You can now sort the list of tags in the Container Registry tag details page by name.

Previously, there was no sort functionality. This sometimes required you to scroll through many pages to find a specific tag.

By default, the tags list is now sorted by name in ascending order. You may also change the sort order to descending.
See this issue to track any further work on tag sorting.

See Documentation and Issue.

like image 24
VonC Avatar answered Oct 05 '22 04:10

VonC