Updating all your Docker images in one swoop.

I’ve been playing around with Docker a lot recently, and one thing that seems to be lacking is a way to update all of your downloaded images in an “apt-get upgrade” fashion. I know this isn’t usually needed in most cases, but in a home use case for Docker most of the time, you can upgrade everything in one go and not worry about it. So I wrote some quick one-line scripts in Bash and PowerShell that can be used to update all your docker images.

Both of these can be run in one line by omitting the carriage returns or put into a script file to reduce some typing. I use both bash and PowerShell sometimes but couldn’t find an obvious way to make one script that worked in both. First up, the PowerShell:

foreach ($i in $(docker images --format "{{.Repository}}:{{.Tag}}")) 
 docker pull $i 

and the very similar Bash variant:

for i in $(docker images --format "{{.Repository}}:{{.Tag}}") 
 docker pull $i 

The bit of the magic part is getting a list of the images that we can easily pass to the docker pull command. Since it takes exactly one parameter, we have to loop through the list with a for/foreach loop. That magic is handled by re-formatting the output of the docker images command to look like <repo name>:<tag>. The ‘–format’ option takes a Go template to format the output however you need to. Another good option would be to add a filter so you don’t try pulling images you might not want to like –filter “dangling=false”.

This should work just fine with any custom registries, but you might want to setup the authentication beforehand with the “docker login” command.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.