24/7/365 Support

Managing LXD containers – advanced options in Ubuntu

In this recipe, we will learn about some advanced options provided by LXD.

How to do it…

Follow these steps to deal with LXD containers:

Sometimes, you may need to clone a container and have it running as a separate system. LXD provides a copy command to create such clones:

$ lxc copy c1 c2 # lxc copy source destination

You can also create a temporary copy with the --ephemeral flag and it will be deleted after one use.

Similarly, you can create a container, configure it as per you requirements, have it stored as an image, and use it to create more containers. The lxc publish command allows you to export existing containers as a new image. The resulting image will contain all modifications from the original container:

$ lxc publish c1 --alias nginx # after installing nginx

The container to be published should be in the stopped state. Alternatively, you can use the --force flag to publish a running container, which will internally stop the container before exporting.

You can also move the entire container from one system to another. The move command helps you with moving containers across hosts. If you move a container on the same host, the original container will be renamed. Note that the container to be renamed must not be running:

$ lxc move c1 c2 # container c1 will be renamed to c2

Finally, we have the snapshot and restore functionality. You can create snapshots of the container or, in simple terms, take a backup of its current state. The snapshot can be a stateful snapshot that stores the container's memory state. Use the following command to create a snapshot of your container:

$ lxc snapshot c1 snap1 # lxc snapshot container cnapshot

The lxc list command will show you the number of snapshots for a given container. To get the details of every snapshot, check the container information with the lxc info command:

$ lxc info c1

...

Snapshots:

c1/shap1 (taken at 2016/05/22 10:34 UTC) (stateless)

Once you have the snapshots created, you can restore it to go back to a point or create new containers out of your snapshots and have both states maintained. To restore your snapshot, use lxc restore, as follows:

$ lxc restore c1 snap1 # lxc restore container snapshot

To create a new container out of your snapshot, use lxc copy, as follows:

$ lxc copy c1/snap1 c4 # lxc copy container/snapshot new_container

When you no longer need a snapshot, delete it with lxc delete, as follows:

$ lxc delete c1/snap1 # lxc delete container/snapshot

How it works…

Most of these commands work with the rootfs or root filesystem of containers. The rootfs is stored under the /var/lib/lxd/containers directory. Copying creates a copy of the rootfs while deleting removes the rootfs for a given container. These commands benefit with the use of the ZFS file system. Features such as copy-on-write speed up the copy and snapshot operations while reducing the total disk space use.

Help Category:

What Our Clients Say