Friday, February 12, 2021

PowerShell script to remove old Docker Images without deleting the base OS image

 By Steve Endow

If you are creating Business Central Docker Containers and regularly updating the artifacts and images for your containers, you may be familiar with this problem.

When you type "docker images" in PowerShell, you are greeted with a pile of old unused images consuming tons of disk space.

A digital junk drawer

I want to delete the old, unused Docker images.  But I do not want to delete the 10.4 GB "base OS" image that is listed at the very bottom.  As long as my OS version remains the same, that image can be used repeatedly, and if I deleted it, I would have to download it again.

I thought that I had not yet figured out how to clean these up via PowerShell, but our BC Community Librarian Natalie Karolak reminded me that I had already created such a script and had blogged about it!

What?

Sure enough, in August 2020 I wrote a blog post where I included a bunch of PowerShell commands for some reason.  One of those commands was a small script to clean up all Docker Images except for the "generic" image.

https://blog.steveendow.com/2020/08/updating-from-navcontainerhelper-to-new.html

This is the script that I documented at that time based on the older naming convention of the generic OS images:


This convoluted PowerShell script does work, but as of February 2021, the base Business Central OS images no longer have the word "generic" in the image Tag.  As you can see in the screen shot above, the last line is:

 mcr.microsoft.com/businesscentral  10.0.17763.1577  

Given this name change, I believe we'll want to use a value other than "generic" to identify our base OS image.

This script uses "businesscentral" as the keyword to identify the base OS image and exclude that from the Docker image deletion process. 

    docker images --format "{{.Repository}}\t{{.Tag}}\t{{.ID}}" |    
    Select-String "businesscentral" -notMatch |   
    ConvertFrom-CSV -Delimiter "`t" -Header ("Repository","Tag","ID") |   
    Sort-Object Tag | % ID | % { docker rmi $_ }  

However, I ran the older script today, and it did not delete my base OS image.  So I'm thinking that I am still missing some detail about how docker rmi handles the base OS image, and whether that image is considered a dependency of the active BC image.


Krzysztof Bialowas reminded me about one additional BC Docker housekeeping command.  

If you're concerned about cleaning up old files on your BC Docker machine, you should also consider the Flush-ContainerHelperCache command in BC Container Helper.

 Flush-ContainerHelperCache -cache bcartifacts -keepDays 7  

Freddy mentions this command in his blog post here, explaining that the command removes old files from the several difference cache folders used by BC Container Helper:

https://freddysblog.com/2019/09/08/containerhelper-0-6-4-1/


If you have any insights to share or see any mistakes I may have made, please let me know.  Otherwise, I hope this helps!


Steve Endow is a Microsoft MVP in Los Angeles.  He works with Dynamics 365 Business Central, Microsoft Power Automate, Power Apps, Azure, and .NET.

You can also find him on Twitter and YouTube

No comments:

Post a Comment

All comments must be reviewed and approved before being published. Your comment will not appear immediately.

How many digits can a Business Central Amount field actually support?

 by Steve Endow (If anyone has a technical explanation for the discrepancy between the Docs and the BC behavior, let me know!) On Sunday nig...