LXC/LXD containers
Table of contents
- Introduction to system containers
- Installing LXD
- Container management
- Images and profiles
- Network and storage
- Practical exercises
1 - Introduction to system containers
LXC vs Docker
| Aspect | LXC/LXD | Docker |
|---|---|---|
| Type | Full system container | Application container |
| Init | systemd/init | PID 1 = application |
| Services | Multiple | Single (ideally) |
| Usage | Lightweight VM | Microservices |
| Images | Full OSes | Application layers |
LXC/LXD use cases
- Isolated development environments
- Migration from VMs
- Multi-distribution testing
- Isolating traditional services
🔝 Back to table of contents
2 - Installing LXD
Installation via snap (recommended)
# Ubuntu/Debian avec snap
snap install lxd
# Ajouter l'utilisateur au groupe
usermod -aG lxd $USER
newgrp lxd
Initialization
# Configuration interactive
lxd init
# Réponses typiques:
# - Clustering: no
# - Storage backend: dir (ou zfs, btrfs)
# - Network: yes, default bridge
# - Remote access: no
# - Auto-update images: yes
Automatic configuration
# Configuration via preseed
cat <<EOF | lxd init --preseed
config: {}
networks:
- config:
ipv4.address: 10.10.10.1/24
ipv4.nat: "true"
ipv6.address: none
description: ""
name: lxdbr0
type: bridge
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
EOF
Verification
# Statut
lxc version
lxd --version
# Info système
lxc info
# Lister les conteneurs
lxc list
🔝 Back to table of contents
3 - Container management
Create and launch
# Créer et démarrer
lxc launch ubuntu:22.04 mycontainer
# Créer sans démarrer
lxc init ubuntu:22.04 mycontainer
# Depuis une image spécifique
lxc launch images:debian/12 debian-container
lxc launch images:alpine/3.18 alpine-container
Basic commands
# Lister
lxc list
lxc list --columns "ns46tS"
# Démarrer/Arrêter
lxc start mycontainer
lxc stop mycontainer
lxc restart mycontainer
# Forcer l'arrêt
lxc stop mycontainer --force
# Supprimer
lxc delete mycontainer
lxc delete mycontainer --force # Si running
Accessing containers
# Shell interactif
lxc exec mycontainer -- bash
lxc exec mycontainer -- /bin/sh
# Exécuter une commande
lxc exec mycontainer -- apt update
lxc exec mycontainer -- cat /etc/os-release
# En tant qu'utilisateur spécifique
lxc exec mycontainer -- su - username
File transfer
# Push (local → conteneur)
lxc file push local-file.txt mycontainer/root/
# Pull (conteneur → local)
lxc file pull mycontainer/etc/passwd ./
# Éditer directement
lxc file edit mycontainer/etc/hosts
Information
# Infos détaillées
lxc info mycontainer
# Configuration
lxc config show mycontainer
# Ressources utilisées
lxc info mycontainer --resources
Snapshots
# Créer
lxc snapshot mycontainer snap1
# Lister
lxc info mycontainer # Section Snapshots
# Restaurer
lxc restore mycontainer snap1
# Supprimer
lxc delete mycontainer/snap1
🔝 Back to table of contents
4 - Images and profiles
Image management
# Lister les images locales
lxc image list
# Chercher des images distantes
lxc image list images: debian
lxc image list ubuntu:
# Télécharger une image
lxc image copy ubuntu:22.04 local: --alias ubuntu22
# Infos sur une image
lxc image info ubuntu22
# Supprimer
lxc image delete ubuntu22
# Créer une image depuis un conteneur
lxc publish mycontainer --alias my-custom-image
Image servers
| Server | URL | Content |
|---|---|---|
ubuntu: | cloud-images.ubuntu.com | Official Ubuntu |
images: | images.linuxcontainers.org | Multi-distros |
Profiles
# Lister les profils
lxc profile list
# Voir un profil
lxc profile show default
# Créer un profil
lxc profile create myprofile
# Éditer
lxc profile edit myprofile
# Appliquer à un conteneur
lxc profile add mycontainer myprofile
lxc launch ubuntu:22.04 myc --profile default --profile myprofile
Profile example
# lxc profile edit myprofile
config:
limits.cpu: "2"
limits.memory: 2GB
description: Profil avec limites CPU et RAM
devices:
root:
path: /
pool: default
size: 10GB
type: disk
eth0:
name: eth0
network: lxdbr0
type: nic
name: myprofile
Resource limits
# CPU
lxc config set mycontainer limits.cpu 2
# Mémoire
lxc config set mycontainer limits.memory 2GB
# Disque (avec profile ou device)
lxc config device set mycontainer root size=20GB
🔝 Back to table of contents
5 - Network and storage
Networks
# Lister les réseaux
lxc network list
# Créer un réseau bridge
lxc network create mybr0 ipv4.address=10.20.30.1/24 ipv4.nat=true
# Attacher à un conteneur
lxc network attach mybr0 mycontainer eth1
# Configuration du conteneur
lxc config device add mycontainer eth1 nic network=mybr0
Static IP
# Via config
lxc config device override mycontainer eth0 ipv4.address=10.10.10.100
# Ou dans le conteneur
lxc exec mycontainer -- bash
# Configurer via netplan ou /etc/network/interfaces
Storage pools
# Lister les pools
lxc storage list
# Créer un pool ZFS
lxc storage create mypool zfs
# Créer un pool dir
lxc storage create dirpool dir source=/data/lxd
# Utiliser le pool
lxc launch ubuntu:22.04 myc --storage mypool
Storage volumes
# Créer un volume
lxc storage volume create default myvolume
# Attacher à un conteneur
lxc config device add mycontainer data disk pool=default source=myvolume path=/data
# Lister
lxc storage volume list default
# Copier un volume
lxc storage volume copy default/myvolume default/myvolume-backup
Mounts from the host
# Monter un répertoire
lxc config device add mycontainer shared disk source=/home/user/shared path=/shared
# Supprimer le montage
lxc config device remove mycontainer shared
🔝 Back to table of contents
6 - Practical exercises
Exercise 1: Web container
Create an Ubuntu container with Nginx installed:
Solution
# Créer le conteneur
lxc launch ubuntu:22.04 webserver
# Installer nginx
lxc exec webserver -- apt update
lxc exec webserver -- apt install -y nginx
# Vérifier
lxc exec webserver -- systemctl status nginx
# Obtenir l'IP
lxc list webserver
Exercise 2: Resource limits
Create a container limited to 1 CPU and 512MB of RAM:
Solution
# Méthode 1 : à la création
lxc launch ubuntu:22.04 limited -c limits.cpu=1 -c limits.memory=512MB
# Méthode 2 : après création
lxc launch ubuntu:22.04 limited
lxc config set limited limits.cpu 1
lxc config set limited limits.memory 512MB
# Vérifier
lxc config show limited
Quiz
Q1. Which command gives you access to a container's shell?
Answer
lxc exec nom-conteneur -- bash
Q2. What is the main difference between LXC and Docker?
Answer
LXC provides full system containers (like lightweight VMs with init), whereas Docker provides application containers (one process per container).
🔝 Back to table of contents
Key takeaways
- LXC/LXD = system containers (lightweight VMs)
lxc launch image nomto create a containerlxc execto run commands- Profiles to reuse configurations
limits.cpuandlimits.memoryfor resources- Snapshots to save the state
- Volumes for persistent storage