4

What is the Docker way to clean up all stopped Docker containers but retain data-only containers?

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

How to clean up the according images?

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
  • 3
    How do you define a data-only container ? – user2915097 Jan 13 '15 at 19:54
  • It is a concept where a container does not run a lengthy process, but only defines a volume used by other containers via `--volumes-from`. In that way, these data containers do not need to be updated when an application changes and can be treated separately. (see related question: http://stackoverflow.com/q/21597463/1725096) – Jens Piegsa Jan 20 '15 at 07:55
  • 1
    Update note: the introduction of **named volumes** renders the concept of **data-only containers** obsolete now. – Jens Piegsa Jan 26 '17 at 13:45

3 Answers3

5

In general there is no definitive way to distinguish data-only from other containers. If you wish them to survive your cleansing, you could probably design a certain name scheme and have more elaborate scripts that wouldn't remote containers with name, say, starting with data-.

Mykola Gurov
  • 8,517
  • 4
  • 29
  • 27
4

Following Mykolas proposal I introduced a naming convention requiring all data-only containers to be suffixed by -data.

To remove all stopped containers, except those named *-data:

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

To remove all unused images afterwards:

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

(the images used by the data-only containers are retained)

Community
  • 1
  • 1
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
3

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

user2915097
  • 30,758
  • 6
  • 57
  • 59