If you want to prefer a simple AWK
approach, here Is my take:
docker rm -f $(docker ps | awk '{ if($2 == "<your image name>") { print $NF}}')
$(docker ps | awk '{ if($2 == "<your image name>") { print $NF}}')
- prints the docker container names based on input image
docker ps
- list all containers
awk '{ if($2 == "<your-image-name>") { print $NF}}'
- The second parsed column of docker ps
gives the image name. Comparing it with your image name will execute print $NF
which prints the container name.
docker rm -f
removes the containers
For example, removing all running containers of ubuntu image, can be done simply as:
docker rm -f $(docker ps | awk '{ if($2 == "ubuntu:latest") { print $NF}}')
PS: Remember to include the image tag in AWK, since it's a equal comparator.