TheJediCowboy
TheJediCowboy

Reputation: 9232

Shell script to cleanup specified docker containers using grep

I want to write a script that is executed by my development build server that will remove any 'similar' docker containers before building and running a new container.

Below is pseudo code for the bash script I need

var name = $1
var number_of_results = # of containers returned from $(docker ps -a | grep "$name")

if(number_of_result > 0)
      docker rm -f $(docker ps -a | grep "$name")

Upvotes: 1

Views: 358

Answers (1)

anubhava
anubhava

Reputation: 785771

You can just use this script in shell:

name="${1?one argument needed}"

ids=$(docker ps -a | awk -v name="$name" '$NF ~ name{print $1}')
[[ -n $ids ]] && docker rm -f $ids

Upvotes: 3

Related Questions