I am developing a serverless webapp using AWS SAM. It has Lambda functions that are built using containers to replicate the actual SAM runtime environment.
I recently stumbled upon issues about SAM builds crashing with the dreaded No space left on device error. Turned out that SAM regularly downloads updated versions of the runtime Docker images, and does not clean up the older ones afterwards.
A listing of the Docker images on my system has shown that I had a dozen of the same one, for instance a nodejs-16 environment. I had to manually purge the superfluous images one by one, and reclaimed about 30 GB on my hard drive by doing so. This is enormous.
Is there a straightforward way using SAM to purge the old images, or instruct it to do so automatically?
Seems that there is no such built-in mechanism yet.
So, I worked my way as follows.
Word of caution: this solution fits my use case but may be destructive if you are not careful. My main use of Docker is for Lambda, and a couple extra containers for a local development database server. Read the commands and understand what they do first.
First, delete all the leftover containers from previous
aws sam localoraws sam start-apiruns. They have in common that the command that runs them is/var/rapid/aws-lambda-rieThen, we can purge the dangling images i.e. those not attached to a container anymore
Alternately, it is possible to do the following to purge the leftover images. But I am not sure about the blast radius in case you have other Docker stuff lying around you may want to keep.
Enjoy the reclaimed gigabytes!