user3142695
user3142695

Reputation: 17342

How to check for existing docker image in terminal?

I'm creating a new tag from an existing image. But sometimes the image is missing, and the command fails. So I need to check if an docker image is existing at all before running the command:

$ docker tag source:anything target:something

But how do I check for existing docker image and how do I use an if-statement correctly in the terminal?

if [docker source:anything] docker tag source:anything target:something fi

Upvotes: 0

Views: 182

Answers (1)

Abslen Char
Abslen Char

Reputation: 3135

this is the correct way to do it

if [[ "$(docker images -q myimage:mytag 2> /dev/null)" == "" ]]; then

// do something

fi

you could also use

[ ! -z $(docker images -q myimage:mytag) ] || echo "does not exist"

Upvotes: 1

Related Questions