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?

1

There are 1 best solutions below

0
DavGin On

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 local or aws sam start-api runs. They have in common that the command that runs them is /var/rapid/aws-lambda-rie

docker rm $(docker ps -a --format '{{.ID}} {{.Command}}' --no-trunc | grep "/var/rapid/aws-lambda-rie" | cut -d ' ' -f 1)

Then, we can purge the dangling images i.e. those not attached to a container anymore

docker rmi $(docker images --filter "dangling=true" -q)

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.

docker image prune -a

Enjoy the reclaimed gigabytes!