2424use CliTools \Shell \CommandBuilder \CommandBuilder ;
2525use Symfony \Component \Console \Input \InputInterface ;
2626use Symfony \Component \Console \Output \OutputInterface ;
27+ use Symfony \Component \Console \Input \InputOption ;
2728
2829class CleanupCommand extends AbstractCommand
2930{
@@ -34,7 +35,13 @@ class CleanupCommand extends AbstractCommand
3435 protected function configure ()
3536 {
3637 $ this ->setName ('docker:cleanup ' )
37- ->setDescription ('Cleanup docker environment ' );
38+ ->setDescription ('Cleanup docker environment ' )
39+ ->addOption (
40+ 'force ' ,
41+ 'f ' ,
42+ InputOption::VALUE_NONE ,
43+ 'Force deletion '
44+ );
3845 }
3946
4047 /**
@@ -59,8 +66,23 @@ protected function cleanDockerImages()
5966 {
6067 $ this ->output ->writeln ('<h2>Cleanup orphaned docker images</h2> ' );
6168 try {
62- $ command = new CommandBuilder ('docker ' , 'rmi $(docker images -qf "dangling=true") ' );
63- $ command ->executeInteractive ();
69+ $ command = new CommandBuilder ('docker ' , 'images -qf "dangling=true" ' );
70+ $ imageList = $ command ->execute ()->getOutput ();
71+
72+ if (!empty ($ volumeList )) {
73+ $ this ->output ->writeln ('<p>Found ' . number_format (count ($ imageList )) . ' images for cleanup</p> ' );
74+
75+ while (!empty ($ imageList )) {
76+ $ command = new CommandBuilder ('docker ' , 'rmi ' );
77+ if ($ this ->input ->getOption ('force ' )) {
78+ $ command ->addArgument ('--force ' );
79+ }
80+ $ command ->addArgumentList (array_splice ($ imageList , 0 , 50 ));
81+ $ command ->executeInteractive ();
82+ }
83+ } else {
84+ $ this ->output ->writeln ('<p>No images for cleanup found</p> ' );
85+ }
6486
6587 } catch (\Exception $ e ) {
6688 $ this ->output ->writeln ('<comment>Some images could not be removed (this is normal)</comment> ' );
@@ -75,14 +97,28 @@ protected function cleanDockerImages()
7597 protected function cleanDockerVolumes ()
7698 {
7799 $ this ->output ->writeln ('<h2>Cleanup orphaned docker volumes</h2> ' );
78- $ this ->output ->writeln ('<info>Updating docker image "martin/docker-cleanup-volumes"</info> ' );
79100
80- $ command = new CommandBuilder ('docker ' , 'pull martin/docker-cleanup-volumes ' );
81- $ command ->executeInteractive ();
101+ try {
102+ $ command = new CommandBuilder ('docker ' , 'volume ls -qf "dangling=true" ' );
103+ $ volumeList = $ command ->execute ()->getOutput ();
104+
105+ if (!empty ($ volumeList )) {
106+ $ this ->output ->writeln ('<p>Found ' . number_format (count ($ volumeList )) . ' volumes for cleanup</p> ' );
82107
83- $ this ->output ->writeln ('<info>Run docker image "martin/docker-cleanup-volumes"</info> ' );
84- $ command = new CommandBuilder ('docker ' , 'run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes ' );
85- $ command ->executeInteractive ();
108+ while (!empty ($ volumeList )) {
109+ $ command = new CommandBuilder ('docker ' , 'volume rm ' );
110+ if ($ this ->input ->getOption ('force ' )) {
111+ $ command ->addArgument ('--force ' );
112+ }
113+ $ command ->addArgumentList (array_splice ($ volumeList , 0 , 50 ));
114+ $ command ->executeInteractive ();
115+ }
116+ } else {
117+ $ this ->output ->writeln ('<p>No volumes for cleanup found</p> ' );
118+ }
119+ } catch (\Exception $ e ) {
120+ $ this ->output ->writeln ('<comment>Some volumes could not be removed (this is normal)</comment> ' );
121+ }
86122
87123 $ this ->output ->writeln ('' );
88124 }
0 commit comments