Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if docker machine exists programmatically?

I'm using docker-machine to manage my cloud servers. I'm writing some bash scripts to automate some tasks. The question is: "How to check in bash script if docker machine with the specific name already exists?". I need some expression to return true if it exists and false if it doesn't.

Thanks

like image 906
wildsurfer Avatar asked Jun 10 '15 14:06

wildsurfer


2 Answers

Just run it through grep, if a regexp is good enough for you. For example if you have a machine called foo:

$ docker-machine ls -q | grep '^foo$'

Should work and return 0. The caret matches the start of the line and the space avoids partial matches. If it doesn't match you will get a non-zero return code.

like image 113
Adrian Mouat Avatar answered Oct 04 '22 00:10

Adrian Mouat


You can use something like the following:

docker-machine status some-machine 2> /dev/null || echo "Machine does not exists"
like image 30
Luís Bianchin Avatar answered Oct 04 '22 00:10

Luís Bianchin