Docker: flush all stopped containers except data-only containers

What is the Docker method for clearing all stopped Docker containers, but storing containers for data only?

  • docker rm $(docker ps -qa -f status=exited) removes them too!

How to clear relevant images?

+4
source share
3 answers

Next Offer Mykolas . I entered a naming convention in which all data-only containers must be a suffix -data.

To remove all stopped containers except those called *-data:

docker ps -a -f status=exited | grep -v '\-data *$'| awk '{if(NR>1) print $1}' | xargs -r docker rm

To delete all unused images:

docker rmi $(docker images -qa -f dangling=true)

(images used by data-only containers are saved)

+4
source

, data-only . , , , , , , , data-.

+5

Maybe you can docker runadd in the command of your entire container for data only a -e "type=data-only"and then filter based on this criterion, either with grep or with an example docker inspect, I start the container with sudo docker run -it -e type=data-only ubuntu bash root@f7e9ea4efbd9:/#and then sudo docker inspect -f "{{ .Config.Env }}" f7eshows [type=data-only PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]

+3
source

All Articles