PowerShell: Remove Docker containers and images

Petr Kostelanský | 11 March 2019
I would like to share with you Powershell script for handling Docker containers and images especially removing them. This script saved me a lot of time because I need relatively often recreate docker images to get new changes inside.

There is a whole script that will stop all Docker containers than remove them and also remove our Docker images we defined in the array $dockerImagesToRemove.

Create new PowerShell script remove-docker-images.ps1 and copy paste all script below:

Function Remove-DockerContainers
{
    # save containers ids into array (--quiet mean return just containers ids)
    $containersIds = (docker ps --all --quiet); 

    if ($containersIds.Count -gt 0)
    {
        Write-Host ("removing docker containers: ")
        # stop all containers
        docker stop $containersIds

        # remove all containers
        docker rm $containersIds
    }
}

Function Remove-DockerImage
{
    Param (
        [Parameter(Mandatory=$True)]
        [string]$DockerImage
    )

    # try find image(s) by name
    $selectedDockerImages = (docker images --all $DockerImage --quiet)

    if ($selectedDockerImages.Count -gt 0) 
    {
        # remove image(s)
        docker rmi $DockerImage
        Write-Host ($DockerImage + " image was removed") -ForegroundColor Green
    } else {
        # show mesage that docker image does not exist
        Write-Host ($DockerImage + " image not exists")  -ForegroundColor Red
    }
} 

Function Remove-DockerImages
{
    Param (
        [Parameter(Mandatory=$True)]
        [string[]]$DockerImages
    )

    Write-Host ("removing docker images: ")
    foreach ($DockerImage in $DockerImages)
    {
        Remove-DockerImage -DockerImage $DockerImage
    }
}


# Array of docker images we want to remove
$dockerImagesToRemove = @('my-docker-image/name1', 'my-docker-image/name3', 'my-docker-image/name2')

Remove-DockerContainers
Remove-DockerImages -DockerImages $dockerImagesToRemove

Now let's look into it and explain it step by step.

Before we start removing Docker images we need to check if there are running containers. If yes, we need to stop them before and then remove. And that's exactly what I do in Remove-DockerContainers function:

Function Remove-DockerContainers
{
    # save containers ids into array (--quiet mean return just containers ids)
    $containersIds = (docker ps --all --quiet); 

    if ($containersIds.Count -gt 0)
    {
        Write-Host ("removing docker containers: ")
        # stop all containers
        docker stop $containersIds

        # remove all containers
        docker rm $containersIds
    }
}

After the previous step, we can start removing Docker images. The Remove-DockerImage function is called from the loop in which we iterate through an array of Docker image names we have defined in string array $dockerImagesToRemove. 

Function Remove-DockerImage
{
    Param (
        [Parameter(Mandatory=$True)]
        [string]$DockerImage
    )

    # try find image(s) by name
    $selectedDockerImages = (docker images --all $DockerImage --quiet)

    if ($selectedDockerImages.Count -gt 0) 
    {
        # remove image(s)
        docker rmi $DockerImage
        Write-Host ($DockerImage + " image was removed") -ForegroundColor Green
    } else {
        # show mesage that docker image does not exist
        Write-Host ($DockerImage + " image not exists")  -ForegroundColor Red
    }
} 

As you can see above we are also checking if there exists Docker image with the name we passed as a parameter to the Remove-DockerImage function. 

The function Remove-DockerImages just iterate through the array of image names so I will not mention it again.

Due to we defined all our logic into functions we can just call them one by one and all work is done.

$dockerImagesToRemove = @('my-docker-image/name1', 'my-docker-image/name3', 'my-docker-image/name2')

Remove-DockerContainers
Remove-DockerImages -DockerImages $dockerImagesToRemove

Don't remember to change $dockerImagesToRemove array with your Docker image names.

Loading ads...