5

Say I have this image tag "node:9.2" as in FROM node:9.2...

is there an API I can hit to see if image with tag "node:9.2" exists and can be retrieved, before I actually try docker build ...?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • Does this answer your question? [Check if image:tag combination already exists on docker hub](https://stackoverflow.com/questions/32113330/check-if-imagetag-combination-already-exists-on-docker-hub) – mat007 Aug 17 '22 at 09:28

3 Answers3

8

This script will build only if image not exist.

update for V2

function docker_tag_exists() {
    curl --silent -f -lSL https://hub.docker.com/v2/repositories/$1/tags/$2 > /dev/null 
}

Use above function for v2

#!/bin/bash
function docker_tag_exists() {
    curl --silent -f -lSL https://index.docker.io/v1/repositories/$1/tags/$2 > /dev/null 

}

if docker_tag_exists library/node 9.11.2-jessie; then
    echo "Docker image exist,...."
    echo "pulling existing docker..."
    #so docker image exist pull the docker image
    docker pull node:9.11.2-jessie
else 
    echo "Docker image not exist remotly...."
    echo "Building docker image..."
    #build docker image here with absoult or retlative path
    docker build -t nodejs .

fi

With little modification from the link below. If the registry is private u check this link With username and password

Adiii
  • 54,482
  • 7
  • 145
  • 148
3

If you have "experimental": enabled set for the Docker daemon then you can use this command:

docker manifest inspect node:9.2

peetasan
  • 848
  • 11
  • 17
-11

Yes.

docker image pull node:9.2
emory
  • 10,725
  • 2
  • 30
  • 58
  • 1
    hmmm, I don't want to actual download the image, just check if it exists, does this do that? – Alexander Mills Jun 20 '18 at 01:48
  • @AlexanderMills if the image is available it will download the image – emory Jun 20 '18 at 01:53
  • yeah in this case I am just looking to ping the Docker hub server/registry to see if the image exists - this is so I can give the user a good warning if the image doesn't exist, in advance instead of making them wait too long – Alexander Mills Jun 20 '18 at 02:10
  • What if the image exists, but it is private and the user does not have access? I suspect from an API pov it is the same as if the image did not exist. – emory Jun 20 '18 at 03:03
  • Is there a command like this that doesn't require auth? In order to get this to work, I first had to run `docker login -u -p ` ... – Alexander Mills Jun 23 '18 at 03:06
  • @AlexanderMills I seriously doubt it. – emory Jun 26 '18 at 00:07
  • hmmm yeah, contrary evidence would be that I can install Docker images without authenticating. E.g., I can do `docker build` with an image that I don't have on my local machine yet, and still I don't have to auth. So there must be some pull command that doesn't require auth? – Alexander Mills Jun 26 '18 at 01:23
  • If private/img is an image you dont have locally and `docker image pull priv/img` fails bc lack of authentication but you can build a Dockerfile like `FROM priv/img ...` then i am not sure what is the point pf authentication. – emory Jun 26 '18 at 10:03
  • I also use this way because curl responds with 404. Image with tag is definitely exists. – kyb Feb 08 '22 at 13:28