Categories
Linux Tutorials

LXD: How to easily apt update and upgrade all running LXD containers

It is easy to create large number of LXD / LXC containers as it consumes minimal resources. Coupled with btrfs, it consumes minimal hard-disk space also due to de-duplication built-in within btrfs file system. Soon you would find yourself spending a significant amount of time updating (sudo apt update) and upgrading (sudo apt -y upgrade) all these containers (don’t forget: sudo apt -y autoremove). Here is a simple bash-fu magic which you can run from host to update all the containers:
lxc -c ns --format csv ls|grep RUNNING|cut -f1 -d',' | xargs -I'{}' sh -c "echo Updating '{}';lxc exec '{}' -- apt -qq update;lxc exec '{}' -- apt -qq -y upgrade;lxc exec '{}' -- apt -qq -y autoremove"
The code has been tested on Ubuntu 16.04 but it is expected to work on any Ubuntu system starting from 16.04. For previous version(s) use apt-get instead of apt.

4 replies on “LXD: How to easily apt update and upgrade all running LXD containers”

That’s a great example of some black belt CLI kung-fu :P. Thank you! I was looking for such straightforward solution!

Do not use it! 2 of my containers went to bed! The “unifi” package would have created a backup and asked during installation! This solution must be forgotten! Do not be lazy!

I would add that a “dpkg –configure -a” solved the problem. That’s the same amount of time. It is not worth using this again.

Leave a Reply

Your email address will not be published.

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