Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Docker image exists in cloud repo

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 ...?

like image 304
Alexander Mills Avatar asked Nov 07 '22 05:11

Alexander Mills


1 Answers

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

like image 147
Adiii Avatar answered Nov 13 '22 03:11

Adiii