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")
  • If docker ps -a | grep "$name" is getting the results, can you just pipe that to wc -l to get the number? – Eric Renouf Jul 8 '15 at 15:24
  • I should probably add that my scripting skills are awful at best :) I tried running wc -l | $(docker ps -a | grep "$name") and it didn't seem to work. Looks like it just ran the docker ps command – TheJediCowboy Jul 8 '15 at 15:29
  • The pipe would go on the other side for wc -l Information always flows left to right in a pipeline, so try $(docker ps -a | grep "$name" | wc -l) and that should give you just the number of results – Eric Renouf Jul 8 '15 at 16:25

The following script should do what you want :



matchingStarted=$(docker ps --filter="name=$name" -q | xargs)
[[ -n $matchingStarted ]] && docker stop $matchingStarted

matching=$(docker ps -a --filter="name=$name" -q | xargs)
[[ -n $matching ]] && docker rm $matching

Basically, it checks if there is a running container with the provided name, and stop it if it find one. Then, it remove any container with the provided name.

Note: You may want to add some argument validation, as if used without argument, this script will stop all running containers and delete all stopped containers. I did not add it here to keep it simple and easily readable.


You have to take into account at least two cases - removing stopped container, which can be removed with a single command and removing running containers, where the container has to be stopped first before being deleted.

In addition to this, in stead of using grep to find the container name, I would use the filter option of docker ps, that way you won't end up grepping the wrong container just because say the command option matches the name you placed in grep. Here is how I would remove any similar docker containers, strictly follow the below sequence -

  1. Remove running containers
for container_id in $(docker ps  --filter="name=$name" -q);do docker stop $container_id && docker rm $container_id;done
  1. Remove stopped containers, since we have stopped running containers in step 1.
for container_id in $(docker ps  --filter="name=$name" -q -a);do docker rm $container_id;done

The -a option will include all containers, including the stopped ones. Not using -a, the default option, will include only running containers. So on step one, you remove the running containers, and then on step two you proceed with the stopped ones. To remove or stop a container, all you need is the container id, the -q options outputs only the ID.

  • you have "do do" in #1. I tried to edit the post but apparently edits have to be 6 characters to be valid... – Doug Johnson Oct 19 '16 at 19:09
  • @DougJohnson - good catch, fixed. – Daniel t. Oct 20 '16 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.