Docker Images Management

I like to automate every annoying operation.

Sometimes I check the downloaded images from the docker hub and update them or remove the unused ones.

With this two commands I manage the docker images downloaded to be sure that every image is always updated and remove every outdated local copy.

Update all images

docker images | grep -v REPOSITORY | awk ‘{print $1":”$2}’ | xargs -L1 docker pull

This command use grep to get all the output of docker images command except the first line (REPOSITORY TAG IMAGE ID CREATED SIZE), awk translate it in a list of image_name:image_version (i.e. ubuntu:latest, ubuntu:16.04, …) and every element is passed to the docker pull command.

After the execution of this command, we are sure that every tag of every images is updated to the last version available on the docker hub, but if we run now the docker images command we can find also some outdated untagged images.

Remove untagged images

docker images | grep “<none>” | awk ‘{print $3}’ | xargs -L1 docker rmi

With this command we can remove all the untagged images (TAG = <none>) using the IMAGE ID as argument for the docker rmi command.