LXC/LXD

LXD Basics

# Init LXD after installation (first time)
lxd init --minimal

# Run new container
lxc launch ubuntu mycontainer

# List launched containers
lxc list

# Stop/Start container
lxc stop mycontainer
lxc start mycontainer

# Exec command
lxc exec mycontainer -- /bin/bash

# Export container
lxc publish mycontainer --alias mycontainer
lxc image export mycontainer /tmp/

# Import container
lxc image import /tmp/*.tar.gz --alias mycontainer

# List images
lxc image list

# Remove container
lxc delete mycontainer

Privileges

# Create container with security privilege
lxc init ubuntuone mycontainer -c security.privileged=true

# Mount host folder to container
lxc config device add mycontainer foobar disk source=/ path=/mnt/root recursive=true

IMAGES

You can pick LXD images for ubuntu here
You need both the lxd file for metadatas and root file
sudo wget https://cloud-images.ubuntu.com/plucky/current/plucky-server-cloudimg-amd64-lxd.tar.xz -O /var/www/html/plucky.meta.tar.xz
sudo wget https://cloud-images.ubuntu.com/plucky/current/plucky-server-cloudimg-amd64-root.tar.xz -O /var/www/html/plucky.root.tar.xz

Usage example
# Download on target
wget 10.10.14.130/plucky.meta.tar.xz -O plucky.meta.tar.xz
wget 10.10.14.130/plucky.root.tar.xz -O plucky.root.tar.xz

# Import image
lxc image import plucky.meta.tar.xz plucky.root.tar.xz --alias ubuntuplucky

# Create container with security privilege
lxc init ubuntuplucky mycontainer1 -c security.privileged=true
lxc config device add mycontainer1 foobar disk source=/ path=/mnt/root recursive=true

# Start Exec
lxc start mycontainer1
lxc exec mycontainer1 bash
# cd /mnt/root

# Remove
lxc delete mycontainer1

Fedora

You may need some extra steps when using lxc without lxd on other OS than ubuntu
You may need to download images.
To list available images you need to run the following commands
sudo lxc-create -t download -n NULL -- --list
sudo lxc-destroy NULL

Then use the following command to create a new container
sudo lxc-create -t download -n ubuntu-c1 -- -d ubuntu -r plucky -a amd64
sudo lxc-ls

You can either start it and attach
$ sudo lxc-start ubuntu-c1
$ sudo lxc-info ubuntu-c1
Name:           ubuntu-c1
State:          RUNNING
PID:            16063
Link:           veth8yMSUb
TX bytes:      682 bytes
RX bytes:      52 bytes
Total bytes:   734 bytes

$ sudo lxc-attach ubuntu-c1

Or run a command on stopped container
sudo lxc-execute ubuntu-c1 bash
root@ubuntu-c1:/# id
uid=0(root) gid=0(root) groups=0(root)

You can export container as a tarball
It seems that LXD don’t support images from LXC.
sudo lxc-stop -n ubuntu-c1

# Create a tarball of the container's filesystem
sudo tar -czf container1-backup.tar.gz -C /var/lib/lxc/ubuntu-c1/rootfs .

$ ls -lh container1-backup.tar.gz
-rw-r--r--. 1 root root 155M 28 juin  12:22 container1-backup.tar.gz

Other

Set storage if missing
lxc storage list
lxc storage create default dir
lxc profile device add default root disk path=/ pool=default

importing cutom metadata.yaml if not present
cat > metadata.yaml << EOF
architecture: x86_64
creation_date: $(date +%s)
properties:
  description: "Ubuntu container"
  os: "ubuntu"
  release: "plucky"
templates:
devices:
  root:
    path: /
    pool: default
    type: disk
EOF
tar -czf metadata.tar.gz metadata.yaml
lxc image import metadata.tar.gz ubuntu_lxc.tar.gz --alias mycontainer