Ubuntu 18.04: Install LXD for container

This article will describe installing LXD and running containers.

Table of Contents

1 Install LXD

Install these packages for LXD.

$ sudo apt install -y lxd lxd-client lxd-tools criu

Install this package if you use ZFS.

$ sudo apt install -y zfsutils-linux

Wait LXD daemon starting.

$ sudo lxd waitready

Initialize LXD with the following command.

$ sudo lxd init
Would you like to use LXD clustering? (yes/no) [default=no]: no
Do you want to configure a new storage pool? (yes/no) [default=yes]:
Name of the new storage pool [default=default]: default
Name of the storage backend to use (dir, lvm, zfs) [default=zfs]: dir
Would you like to connect to a MAAS server? (yes/no) [default=no]: no
Would you like to create a new network bridge? (yes/no) [default=yes]:
What should the new bridge be called? [default=lxdbr0]: lxdbr0
What IPv4 address should be used? (CIDR subnet notation, “auto” or
“none”) [default=auto]: auto
What IPv6 address should be used? (CIDR subnet notation, “auto” or
“none”) [default=auto]: none
Would you like LXD to be available over the network? (yes/no)
[default=no]: no
Would you like stale cached images to be updated automatically?
(yes/no) [default=yes] yes
Would you like a YAML "lxd init" preseed to be printed? (yes/no)
[default=no]: no

You can use pipe for automatic initialization.

$ cat <<EOF | sudo lxd init

The users in lxd group can run lxd without sudo command. After adding user to lxd group, restart machine.

$ sudo gpasswd -a "${USER}" lxd
$ sudo reboot

2 Run container

"lxc launch" is combined with "lxc init" and "lxc start". "lxc exec" runs command on container.

$ lxc launch images:debian/stretch debian
$ lxc exec debian ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
5:eth0@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue
state UP group default qlen 1000
    link/ether 00:16:3e:3c:1d:23 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet brd scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fd42:6bb1:545e:554b:216:3eff:fe3c:1d23/64 scope global
mngtmpaddr dynamic
       valid_lft 3596sec preferred_lft 3596sec
    inet6 fe80::216:3eff:fe3c:1d23/64 scope link
       valid_lft forever preferred_lft forever

"lxc launch" is equal with the following "lxc init" and "lxc start". "lxc init" creates container. "lxc start" starts container.

$ lxc init images:debian/stretch debian
$ lxc start debian
$ lxc exec debian ip a s