LXD/LXC Basic Setup
LXD Initial Setup
sudo usermod -aG lxd <user>
Initialize LXD.
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]: yes
Name of the new storage pool [default=default]: default
Name of the storage backend to use (dir, lvm, zfs, ceph, btrfs) [default=zfs]: dir
Would you like to connect to a MAAS server? (yes/no) [default=no]: no
Would you like to create a new local network bridge? (yes/no) [default=yes]: 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]: auto
Would you like the LXD server 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]: yes
config: {}
networks:
- config:
ipv4.address: auto
ipv6.address: auto
description: ""
name: lxdbr0
type: ""
project: default
storage_pools:
- config: {}
description: ""
name: default
driver: dir
profiles:
- config: {}
description: ""
devices:
eth0:
name: eth0
network: lxdbr0
type: nic
root:
path: /
pool: default
type: disk
name: default
projects: []
cluster: null
List the available images by the command: lxc image list ubuntu:22.04 | grep x86_64
. Then install a certain image and name the container lxc-webserver
, that will be capable to run other containers inside, for more details read the article Nested containers in LXD.
lxc launch ubuntu:22.04 lxc-webserver -c security.nesting=true
Creating lxc-webserver
Starting lxc-webserver
In order to add (or remove) the nesting option to an existing LXC, use:
lxc config set lxc-webserver security.nesting true
lxc list --columns ns4 # name, state and IPv4
+---------------+---------+-----------------------+
| NAME | STATE | IPV4 |
+---------------+---------+-----------------------+
| lxc-webserver | RUNNING | 10.127.198.222 (eth0) |
+---------------+---------+-----------------------+
Configure the container to obtain a static IP assignment.
lxc config device override lxc-webserver eth0
Device eth0 overridden for lxc-webserver
lxc config device set lxc-webserver eth0 ipv4.address 10.127.198.222 # no output mean everything is fine
lxc restart lxc-webserver
lxc list --columns ns4 | grep eth0 # check the ip
| lxc-webserver | RUNNING | 10.127.198.222 (eth0) |
LXD Basic operation
Restart the service.
sudo snap restart lxd
Mount a host directory to a directory inside a container. Map the permissions.
printf "lxd:$(id -u):1\nroot:$(id -u):1\n" | sudo tee -a /etc/subuid # Allow LXD’s use of our user uid
printf "lxd:$(id -g):1\nroot:$(id -g):1\n" | sudo tee -a /etc/subgid # Allow LXD’s use of our user gid
sudo snap restart lxd # Restart LXD to have it load the new map
printf "uid $(id -u) 1000\ngid $(id -g) 1000" | lxc config set lxc-webserver raw.idmap - # Set a custom map for our container
lxc restart lxc-webserver # Restart the container to have the new map apply
lxc config device add lxc-webserver Git disk source=/home/<user>/Git path=/home/<user>/Git # Mount the directory
Do the actual share (mount) of some directories.
lxc config device add lxc-webserver Git disk source=/home/<user>/Git path=/home/<user>/Git
lxc config device add lxc-webserver VSC disk source=/home/<user>/.vscode-server path=/home/<user>/.vscode-server
References:
- Linux containers LXD: Server configuration settings
- Canonical Ubuntu Blog: Custom user mappings in LXD containers
- Canonical Ubuntu Blog: Mounting your home directory in LXD
- Ask Ubuntu: Adding a shared host directory to an LXC/LXD Container (for privileged container)
LXC Basic operations
List available containers.
lxc list
lxc list -c ns4
Login to a container (note lxc-webserver
is a container name).
lxc shell lxc-webserver
Execute a command against the container from the host.
lxc exec lxc-webserver -- apt install apache2
Start, stop or delete container.
lxc (start|stop|delete) container-name
Create a snapshot.
lxc snapshot lxc-webserver snapshot-name
Delete a snapshot.
lxc delete lxc-webserver/snapshot-name
Restore a snapshot.
lxc restore lxc-webserver snapshot-name
Create a backup.
lxc export lxc-webserver ./lxc-webserver-backup.tar.gz
Restore a backup.
lxc import ./lxc-webserver-backup.tar.gz
Get info about the container (and its snapshots at the bottom).
lxc info lxc-webserver
Limit the container's memory usage.
lxc config set lxc-webserver limits.memory 1GB
Auto-start a container.
lxc config set lxc-webserver boot.autostart 1
Set an auto-start delay for a container.
lxc config set lxc-webserver boot.autostart.delay 30
Set an auto-start order number for a container.
lxc config set lxc-database boot.autostart.order 2
lxc config set lxc-webserver boot.autostart.order 3
Disable IPv6 for the containers – reference.
lxc network set lxdbr0 ipv6.address none
References
- DigitalOcean: How To Install and Configure LXD on Ubuntu 20.04
- Linux containers LXD: Getting Started with LXD
- Canonical Ubuntu Server: LXD
- LearnLinuxTV: Getting started with LXD Containerization (Full Guide!)
- Ask Ubuntu: apt-key deprecation warning when updating system
- Ask Ubuntu: At what point is the ~/.bashrc file created?