What are the ways to create an LXC container?

I’ve always been doing it like so:

$ lxc-create -n ... -t centos -- --release ...

But recently I learned that those templates are deprecated. And distrobuilder is to be used.

The templates that’s left are:

  • busybox - Not an option for me. I generally need CentOS, Debian, Ubuntu, whatever.
  • download - I’d like to have OS as close to original as possible. Not sure if that’s the case with pre-built images. What do you say? And I generally use privileged containers if that matters.
  • local - This one consumes images, one can create with distrobuilder as I now see. The ones you create with e.g. distrobuilder build-lxc /usr/share/distrobuilder/centos (creates meta.tar.xz and rootfs.tar.xz).
  • oci - This allows to make use of Docker containers. But I generally need just some plain stock Ubuntu, or CentOS. So doesn’t seem to be useful to me at least for now, does it?

And there’s distrobuilder. build-dir just creates rootfs. build-lxc creates archived rootfs (rootfs.tar.xz) and supposedly config (meta.tar.xz). Which I can use with lxc-create -t local. pack-lxc probably creates rootfs.tar.xz and meta.tar.xz from rootfs dir.

Okay, I now see that I can probably create LXC container with

$ distrobuilder build-lxc /usr/share/distrobuilder/centos
$ lxc-create -t local -- --fstree rootfs.tar.xz --metadata meta.tar.xz`

But how do I configure it? I see 2 * 5 configs in meta.tar.xz. What are the other ways to create LXC container? What needs to be done to be able to lxc-start LXC container? Let’s leave deprecated templates out of the discussion. I’d like to learn the new way. Thanks in advance.

What would you like to configure?

That lxc-create should have given you a working container, the same way that lxc-create -t centos -n blah would have in the past and similar to what lxc-create -t download -n blah -- -d centos -r 7 -a amd64 would have in 2.0 and still now in 3.0.

1 Like

What would you like to configure?

Occasionally I need a static (predictable) IP. That’s what I might need changed in LXC container config (lxc.net).

So, there are basically two ways to create LXC container with, say, CentOS? One way is,

$ distrobuilder build-lxc /usr/share/distrobuilder/centos
$ lxc-create -t local -- --fstree rootfs.tar.xz --metadata meta.tar.xz

distrobuilder will create an image with minor corrections (compared to official distribution) specified in /usr/share/distrobuilder/centos. lxc-create would copy it to /var/lib/lxc and create a config. Correct me whenever I’m wrong, please.

The other is,

$ lxc-create -t download -n blah -- -d centos -r 7 -a amd64

Where I get an image not much different from the previous way.

What made me alert is this,

Note that the images found on this image server are unofficial images. Whenever possible, you should try to use official images from your Linux distribution of choice. Images from this server are provided as a convenience with no guarantee whatsoever.

So, in the first case, changes made to an image downloaded from distribution’s server can be found in /usr/share/distrobuilder/centos. What about lxc-create -t download? Somewhere in this repo? Can you point at probably entrypoints? Relevant files?

Also, is build-lxc basically build-dir + pack-lxc?

The images at https://images.linuxcontainers.org are built by our Jenkins server at https://jenkins.linuxcontainers.org using the scripts found at https://github.com/lxc/lxc-ci and the LXC template scripts from https://github.com/lxc/lxc-templates. That latter part is changing as we’re moving those images to using distrobuilder too, right now Debian and Ubuntu have been switched over with the distrobuilder yaml files available in the lxc-ci repository.

build-lxc is indeed equivalent of build-dir + pack-lxc, most users won’t need to use the build-dir action. That action was added mostly for our own needs where we build and publish images for both LXC and LXD.

Config customization for LXC should be done in /var/lib/lxc/CONTAINER-NAME/config as was the case before.
If you want a change to affect all newly created containers you may put it in /etc/lxc/default.conf.
If there’s something you wish all your containers to have (existing and future), you can put it in a file in /usr/share/lxc/config/common.conf.d/

1 Like

Sorry, for probably being persistent. But from what I can see CentOS minimal image is 906M large: http://centos.uib.no/7/isos/x86_64/, but with download template I get:

67M     /var/cache/lxc/download/centos/7/amd64/default/rootfs.tar.xz

Can you explain how it works? Is it going to change with distrobuilder?

The CentOS link you posted is from an ISO, which contains all the necessary packages and files to install a distribution. The container image only contains a small set of packages that allow you to run the distribution, that is why there is that big difference on the size.

I hate to hijack this thread, and if I’m off-base, please feel free to delete my post and tell me to start a new thread.
Regarding this discussion, I’m trying to figure out if there is a means to create a CentOS 7 container image for the armhf architecture? I’m so new to container technologies, so please forgive my lack of knowledge. Running LXD on Ubuntu 18.04 on a rather low power Raspberry Pi 3. Actually runs rather well, but have a need for a CentOS container. Was able to deploy via Docker, but no LXC\LXD options seem to exist for CentOS on armhf currently. If it’s possibly for someone to spin their own container image for CentOS armhf, might there be some guides or howto’s on the subject? Thanks in advance…

If you can first create a CentOS container using distrowatch, then it is very easy to add the container image into LXD and launch containers from that container image.

Hello all,

I am trying to create a custom image for LXC container. I have used this command
$ lxc-create -t local – --fstree rootfs.tar.xz --metadata meta.tar.xz`

But instead of creating by itself, may be it is asking for some input which I am not sure what to give input. Can you please tell me how can I create a container for a custom image. If it is possible, please explain with a example container creation like this https://blog.simos.info/using-distrobuilder-to-create-container-images-for-lxc-and-lxd/ post.
Thanks in advance.

What exact commands? What input?

The command which I have used to create LXC container image is,

       $ lxc-create -t local -- --fstree rootfs.tar.xz --metadata meta.tar.xz`

and after hitting enter this is the screen

So I am not understanding what I should write there.
Also. I want to know how can I create a custom image for LXC container.

Run the command without the trailing backtick (`). It waits for you to type the closing backtick, but backticks are not needed here.

yes I tried that as well. it is giving me an error

I have searched to solve the error but I could not get any solution.

Are you sure that the rootfs.tar.xz and meta.tar.xz files exist on the directory where you are running?

this problem is fixed now. actually the rootfs.tar.xz and meta.tar.xz files do not exist on the directory before. when the image file is complied, these two rootfs.tar.xz and meta.tar.xz creates.