Is it possible to create an LXC container that you maintain only for the purpose of copying as a template? If so, how does one properly copy an LXC container?
3 Answers
Yes, an LXC container is some config files and a directory within the whole server. If you copy this directory and config files and adjust the parameters you can use it as a template. Just tar it and untar it to the new machine's directory.
-
Where do I configure the cgroup? The copy is trying to use the same one as my "original" Commented Dec 21, 2011 at 16:16
-
This can be easily obtained using BTRFS filesystem in combination with snapshots.
The template filesystem must reside in a subvolume on a btrfs filesystem, say
btrfs subvolume create /mnt/btrfs/template
After which you can populate it.
To create the snapshot:
btrfs subvolume snapshot /mnt/btrfs/template /mnt/btrfs/node-1
Afterwards remove the snapshot with:
btrfs subvolume delete delete /mnt/btrfs/node-1
Advantages are that:
- creation and removal is fast
- no extra space is needed, only changed files (blocks) occupy extra space
Further on, if you want to provide your container with some writable private directories on top of a read-only template filesystem you could use mount -o bind from within the container
mount -o bind /mnt/btrfs/node-1/var /var
I also use a main template for all my containers.
If you are using Ubuntu 14.04, lxc-clone is your friend to create new container based off a template.
lxc-clone -o template_container -n new_container