Last active
September 16, 2018 17:16
-
-
Save smartpcr/ad40c1109bc1e34457ec2643e52e1bc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| param( | |
| [string]$imageToRemove = "", | |
| [string]$containersToRemove = "" | |
| ) | |
| Write-Host "Checking existing docker containers..." | |
| $containers = Invoke-Expression "docker ps -q" | |
| if ($containersToRemove -and $containersToRemove.Length -gt 0) { | |
| $containers = Invoke-Expression "docker ps -q --filter `"$containersToRemove`"" | |
| } | |
| if ($containers) { | |
| $containers | ForEach-Object { | |
| $containerId = $_ | |
| Invoke-Expression "docker kill $containerId" | |
| Invoke-Expression "docker rm $containerId" | |
| } | |
| } | |
| Write-Host "Checking existing images..." | |
| $images = Invoke-Expression "docker images -q" | |
| if ($imageToRemove -and $imageToRemove.Length -gt 0) { | |
| $images = Invoke-Expression "docker images $imageToRemove -q" | |
| } | |
| if ($images) { | |
| $images | ForEach-Object { | |
| $imageId = $_ | |
| Invoke-Expression "docker rmi -f $imageId" | |
| } | |
| } | |
| $danglingImages = Invoke-Expression "docker images -qf dangling=true" | |
| if ($danglingImages) { | |
| Write-Host "remove all dangling images..." | |
| Invoke-Expression "docker rmi -f $(docker images -qf dangling=true)" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment