Remove containers by image name

不打扰是莪最后的温柔 提交于 2020-12-26 11:44:09

问题


I mistakenly created a bunch of containers which I now want to remove. I can list them with:

docker container ls -aq -f "ancestor=portainer/portainer"

How can I "pipe" these container IDs to docker container rm?

What doesn't work:

  • docker ls -aq -f "ancestor=portainer/portainer" | docker container rm
  • docker container rm $(docker ls -aq -f "ancestor=portainer/portainer")
  • docker rm `docker ls -aq -f "ancestor=portainer/portainer"`

回答1:


Try this:

docker container ls -aq -f "ancestor=portainer/portainer" | xargs docker container rm 



回答2:


OK, it seems my mistake was running docker ls instead of docker container ls. I'll leave this question up as I was unable to find any such examples on the WWW.

docker container rm $(docker container ls -aq -f "ancestor=portainer/portainer")



回答3:


You can try this

for c in `docker container ls -aq -f "ancestor=portainer/portainer"`; do
   docker container rm $c
done



回答4:


This will always exit with success error code 0 even if there are no containers to remove:

docker ps -aq -f "ancestor=fakeuser/my-image-name" | xargs -I CID docker rm -f CID

If you want to give informative output in the logs:

docker ps -aq -f "ancestor=fakeuser/my-image-name" | xargs -I CID docker rm -f CID | awk '{print "removed container " $1}'


来源:https://stackoverflow.com/questions/58879058/remove-containers-by-image-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!