Creating an LXC Container on the Raspberry Pi

This post assumes you’ve followed the instructions in our post “Building an LXC-friendly Kernel on the Raspberry Pi” to get kernel support working and install the lxc tools from the LXC git repository.

Mount a cgroup

# pico /etc/fstab

# add the line “lxc /sys/fs/cgroup cgroup defaults 0 0”

# mount -a

Create a Directory to Store Hosts

# mkdir -p /var/lxc/guests

Create a File System for the Container

Let’s create a container called “test”.

First, create a filesystem for the container. This may take some time

# apt-get install debootstrap

# mkdir -p /var/lxc/guests/test

# debootstrap wheezy /var/lxc/guests/test/fs/

Modify the Container’s File System

# chroot /var/lxc/guests/test/fs/

Change the root password.

# passwd

Change the hostname as you wish.

# pico /etc/hostname

undo chroot

# exit

Create a Minimal Configuration File

# pico /var/lxc/guests/test/config

Enter the following:

lxc.utsname = test

lxc.tty = 2

lxc.rootfs = /var/lxc/guests/test/fs

Create the Container

lxc-create -f /var/lxc/guests/test/config -n test

Test the Container

# lxc-start -n test -d

[wait for a while, a few minutes]

# lxc-console -n test -t 1


14 thoughts on “Creating an LXC Container on the Raspberry Pi

  1. I have followed this and the Building an LXC-friendly Kernel guides with no errors until I try to create a container. When I run the lxc-create command I get the following message:

    lxc-create: Error creating container test

    When this happens the file system folder for the container (/var/lxc/guests/test/fs/) is being deleted as well. Any idea what’s going here?

    • Sounds a little strange. Do you check out /var/log/lxc/* ? What exactly was the command you ran, and what were the contents of your file system and configuration file?

      • Thanks for the reply!

        The directory /var/log/lxc/ did not exist, and nothing is being logged there after creating it manually.

        The exact command I ran was
        lxc-create -f /var/lxc/guests/test/config -n test

        The content of /var/lxc/guests/test/config was as specified in this guide:
        lxc.utsname = test
        lxc.tty = 2
        lxc.rootfs = /var/lxc/guests/test/fs

        The content of /var/lxc/guests/test/fs/ was the expected result of running
        debootstrap wheezy /var/lxc/guests/test/fs/
        i.e. a linux file system.

      • Is it possible that you ran lxc-destroy at some point? That would have deleted the folder.

        Looking at the source for LXC, usually there is more than just this error message. Was there another message? If not, I’ll check the above instructions again and get back to you.

  2. No, I did not run lxc-destroy at any point.
    After running the lxc-create command that was the single error message I got.
    I also discovered that it’s logging to /usr/local/var/log/lxc/, but the log only has the same message:
    lxc-create 1379574596.074 ERROR lxc_create – Error creating container test

    I followed this and the building a kernel guide without any exceptions, and had no problems or errors until I ran the lxc-create command.
    This was done on a fresh install using the 2013-07-26-wheezy-raspbian image.

    • Hmm, interesting. That’s a more recent build of Raspbian than we’ve been using. I’ll take a look at this when I have time, probably not for a week though – our students return on Monday and I need to attend to them 🙂

    • Sorry, I missed this. One of my students went through the instructions a while back and had no problems — you might want to give it another go. They may have made some minor tweaks as they went along, but the general procedure is correct.

  3. Pingback: OpenStack on Raspberry Pi: Part 2 – Getting Started | Professional OpenStack

  4. I followed the instructions and did not get any errors. I am however facing the same problem as explained by Mindphaser. Creation of a container requests for a template. I had to run the command:

    lxc-create -n test1 -f /var/lxc/guests/test/fs -t download

    However, the directory “fs” gets deleted when I try to start the container. Was a solution to this problem found out?

    I am using the version of Raspbian mentioned by David i.e. 2013-02-09-wheezy-raspbian, as installing in the latest version was not successful while executing the chroot command.

    Kindly let me know if the solution to this problem was found out.


  5. needed to specify the template lxc-create -f /var/lxc/guests/test/config -t /usr/local/share/lxc/templates/lxc-debian -n test

  6. Hey,

    i have the same error when i try to use the command above:
    root@raspberrypi:$ lxc-create -f /var/lxc/guests/test/config -t /usr/local/share/lxc/templates/lxc-debian – n test
    lxc-create: lxc_create.c: main: 274 Error creating container test

    I checked the LD_LIBRARY_PATH
    root@raspberrypi:/home/pi# echo $LD_LIBRARY_PATH

    Can anyone help me?

    Thanks in advice

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s