Updating local docker images using awk and xargs

Posted in category tips on 2016-07-21
Tags: tips, docker, linux

It is typically not a big deal to update your images with simple docker pull image:latest that will replace your current image with the latest available (if there happen to be a new version available at that time). Then you can clean <none> image that will be produced as a result of the update (this is simply previous version of your image there marked with <none> now). This tip-post describes the use of awk and xargs for automating updating docker images and cleaning up all left <none> images.

The following bash script will download all the latest versions of all images available locally:

docker images \
| awk '(NR > 1) && ($2!~/none/) {print $1":"$2}' \
| xargs -L1 docker pull

And this will cleanup all <none> images that are left after update:

docker images \
| awk '(NR > 1) && ($2~/none/) {print $3}' \
| xargs -L1 docker rmi

And in case you might need to share your new images with a Docker host that does not happen to have access to required docker repository, you can save your updated docker images to files (tar-archives in fact) and simply import into target docker host.

This will save available images as files following naming convention [company_]<imagename>-<tag>.docker:

docker images \
| awk '(NR > 1) && ($2!~/none/) {docker save -o "gensub(/\//,"_","g",$1)"-"$2".docker "$1":"$2}' \
| bash

And for those lucky ones who need to go through Windows share, following script will help you uploading your saved image files to a Windows share (-m SMB2 is there in case it does not work without it - some known bug in smbclient):

smbclient //server/share -U username -W domain -m SMB2 -c 'prompt OFF; recurse ON; mput *.docker'