On a Python project last year, I had set up an Azure DevOps pipeline with the logic to build a Docker image of a Python web application with the following logic:
- When a feature was developed, I would pull a branch from the
develop
and make the modification. - When I finished the feature, I’d create a PR to merge that feature into the
develop
. - Once the PR was merged, the branch policy of
develop
would trigger the pipeline to run and build aready-qa
tagged image to deploy to the testing environment in Azure. - After QA testing, I’d merge
develop
tomain
, triggering another build and tag a new image with the valuelatest
. It’d be the image to deploy to the production environment on Azure.
This created a lot of Docker images because, in addition to the ready-qa
or latest
, I’d have a {build_id}
tag for each run.
In consequences, the Container Registry cost the most in all the resources provisioned to Azure. And the Docker image list would built up over time.
How could I improve by cleaning up the older and useless Docker images?
Brainstroming
The first solution would be to upgrade the Container Registry to a premium one to define a retention policy.
I didn’t want that.
I knew I wanted to keep all non-numeric tags because I had only one of each.
But I could delete all but the last 10 numerically tagged images.
That’s when I thought about the Azure CLI.
The Script
I ask Claude AI to help me out with this.
First, I wondered how to list the numeric tags I wanted:
|
|
This worked well, but as I had dozens of images to delete, I wanted to make efficient.
My requirements were:
- provide the Container Registry as a required input.
- provide an option to dry run the script.
- provide the ability to configure the number of numeric tags to keep.
- ignore the non-numeric tags from deletion.
The first suggestion was to use a Python script. But it required to be connected to Azure. It required more work.
However, I knew I could run a bash script in the Azure CLI console, here is the script that achieves that:
|
|
The AI made a mistake on the --dry-run
implementation when checking it. In bash, you check a boolean with:
|
|
At first, it told me the following, which isn’t proper bash syntax:
|
|
Beyond A Bash Script
I’d love to run this regularly without me thinking about it. Is the time to build that worth the cost?
Is the cost of a Premium Container Registry higher?
A few weeks following this initial Bash script, I’ve found how to do it. I’ll give you a hint: Runbook.
To be continued…
Follow me
Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.
Photo by Tom Fisk.