LXC (Linux Containers) bietet OS-Level-Virtualisierung für Linux. Im Gegensatz zu Docker sind LXC-Container vollständige System-Container mit eigenem Init-System.

LXC vs Docker

Vergleich

| Merkmal | LXC | Docker | |---------|-----|--------| | Typ | Systemcontainer | Anwendungscontainer | | Init-System | Ja (systemd) | Nein | | Persistenz | Standard | Volumes nötig | | Verwendung | VM-ähnlich | Microservices | | Management | lxc-* Befehle | docker CLI |

Wann LXC?

- VM-ähnliche Isolation gewünscht
- Mehrere Dienste pro Container
- Systemd-basierte Anwendungen
- Langlebige Server-Umgebungen
- Proxmox VE

Installation

Debian/Ubuntu

apt install lxc lxc-templates

CentOS/RHEL

dnf install epel-release
dnf install lxc lxc-templates libvirt

Überprüfen

# LXC-Version
lxc-checkconfig

# Soll alles "enabled" sein

Container erstellen

Mit Template

# Verfügbare Templates
ls /usr/share/lxc/templates/

# Container erstellen
lxc-create -n mycontainer -t debian -- -r bookworm

# Ubuntu
lxc-create -n ubuntu-container -t ubuntu -- -r jammy

# Alpine (minimal)
lxc-create -n alpine-container -t alpine

Container-Verzeichnis

/var/lib/lxc/
├── mycontainer/
│   ├── config
│   └── rootfs/
└── ubuntu-container/
    ├── config
    └── rootfs/

Container verwalten

Grundbefehle

# Starten
lxc-start -n mycontainer

# Stoppen
lxc-stop -n mycontainer

# Status
lxc-info -n mycontainer

# Liste
lxc-ls -f

# Löschen
lxc-destroy -n mycontainer

In Container einloggen

# Attach (Terminal)
lxc-attach -n mycontainer

# Console
lxc-console -n mycontainer

# Befehl ausführen
lxc-attach -n mycontainer -- ls -la /

Konfiguration

Container-Config

# /var/lib/lxc/mycontainer/config

# Grundeinstellungen
lxc.uts.name = mycontainer
lxc.arch = linux64

# Netzwerk
lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:xx:xx:xx
lxc.net.0.ipv4.address = 10.0.3.100/24
lxc.net.0.ipv4.gateway = 10.0.3.1

# Ressourcen
lxc.cgroup2.memory.max = 1G
lxc.cgroup2.cpu.max = 100000 100000

# Autostart
lxc.start.auto = 1
lxc.start.delay = 5

Netzwerk-Modi

| Modus | Beschreibung | |-------|--------------| | veth | Virtual Ethernet (Bridge) | | macvlan | MAC-VLAN (eigene MAC) | | phys | Physisches Interface | | none | Kein Netzwerk |

Bridge konfigurieren

# /etc/lxc/default.conf (Standard für neue Container)

lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:xx:xx:xx
# Bridge erstellen mit Netplan
# /etc/netplan/01-lxc.yaml

network:
  version: 2
  bridges:
    lxcbr0:
      addresses: [10.0.3.1/24]
      interfaces: []
      parameters:
        stp: false
        forward-delay: 0

LXD (Modernes LXC)

Installation

# Snap (empfohlen)
snap install lxd

# Init
lxd init

LXD-Befehle

# Container erstellen
lxc launch ubuntu:22.04 mycontainer

# Liste
lxc list

# Shell
lxc exec mycontainer -- bash

# Stoppen
lxc stop mycontainer

# Löschen
lxc delete mycontainer

Image-Management

# Verfügbare Images
lxc image list images:

# Remote-Images durchsuchen
lxc image list images: alpine

# Image herunterladen
lxc image copy images:alpine/3.19 local: --alias alpine

# Lokale Images
lxc image list local:

Profile

# Profile erstellen
lxc profile create webserver

# Profile bearbeiten
lxc profile edit webserver

# Container mit Profil
lxc launch ubuntu:22.04 web1 -p webserver
# Profil-Inhalt
config:
  limits.cpu: "2"
  limits.memory: 2GB
devices:
  root:
    path: /
    pool: default
    type: disk
  eth0:
    nictype: bridged
    parent: lxcbr0
    type: nic

Ressourcen-Limits

CPU

# LXC
lxc.cgroup2.cpu.max = 50000 100000  # 50% einer CPU

# LXD
lxc config set mycontainer limits.cpu 2
lxc config set mycontainer limits.cpu.allowance 50%

Memory

# LXC
lxc.cgroup2.memory.max = 1G
lxc.cgroup2.memory.swap.max = 512M

# LXD
lxc config set mycontainer limits.memory 1GB

Disk

# LXD mit Storage Pool
lxc config device set mycontainer root size=10GB

Snapshots

Mit LXD

# Snapshot erstellen
lxc snapshot mycontainer snap1

# Snapshots anzeigen
lxc info mycontainer

# Wiederherstellen
lxc restore mycontainer snap1

# Snapshot löschen
lxc delete mycontainer/snap1

Backup

# Container exportieren
lxc export mycontainer mycontainer-backup.tar.gz

# Importieren
lxc import mycontainer-backup.tar.gz

Dateien kopieren

# In Container
lxc file push local-file.txt mycontainer/root/

# Aus Container
lxc file pull mycontainer/etc/hostname ./

# Verzeichnis
lxc file push -r ./mydir mycontainer/root/

Unprivileged Container

Vorteile

- Höhere Sicherheit
- UID/GID-Mapping
- Isolation vom Host

Konfiguration

# /etc/lxc/default.conf

lxc.idmap = u 0 100000 65536
lxc.idmap = g 0 100000 65536
# /etc/subuid
root:100000:65536

# /etc/subgid
root:100000:65536

User-Container

# Als normaler User
mkdir -p ~/.config/lxc
cp /etc/lxc/default.conf ~/.config/lxc/

# Container erstellen
lxc-create -n usercontainer -t download -- -d ubuntu -r jammy -a amd64

Proxmox und LXC

Container in Proxmox

# Template herunterladen
pveam update
pveam available
pveam download local debian-12-standard_12.0-1_amd64.tar.zst

# Container erstellen
pct create 100 local:vztmpl/debian-12-standard_12.0-1_amd64.tar.zst \
    --hostname myct \
    --memory 1024 \
    --cores 2 \
    --rootfs local-lvm:8 \
    --net0 name=eth0,bridge=vmbr0,ip=dhcp

# Starten
pct start 100

# Console
pct enter 100

Proxmox-Befehle

| Befehl | Funktion | |--------|----------| | pct list | Container auflisten | | pct start ID | Starten | | pct stop ID | Stoppen | | pct enter ID | Console | | pct destroy ID | Löschen |

Netzwerk-Beispiele

NAT-Netzwerk

# Bridge mit NAT
iptables -t nat -A POSTROUTING -s 10.0.3.0/24 -o eth0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward

Statische IP

# /var/lib/lxc/mycontainer/config

lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.net.0.ipv4.address = 10.0.3.100/24
lxc.net.0.ipv4.gateway = 10.0.3.1

DHCP

# Im Container
dhclient eth0

# Oder in /etc/network/interfaces
auto eth0
iface eth0 inet dhcp

Troubleshooting

Container startet nicht

# Logs prüfen
lxc-start -n mycontainer -l DEBUG -o /tmp/lxc.log
cat /tmp/lxc.log

# Konfiguration prüfen
lxc-checkconfig

Netzwerk-Probleme

# Bridge prüfen
ip link show lxcbr0

# Im Container
lxc-attach -n mycontainer -- ip addr
lxc-attach -n mycontainer -- ping 8.8.8.8

Berechtigungsfehler

# AppArmor deaktivieren (temporär)
lxc.apparmor.profile = unconfined

# Oder AppArmor-Profil prüfen
aa-status

Zusammenfassung

| Befehl (LXC) | Funktion | |--------------|----------| | lxc-create | Erstellen | | lxc-start | Starten | | lxc-stop | Stoppen | | lxc-attach | Shell | | lxc-destroy | Löschen | | lxc-ls -f | Auflisten |

| Befehl (LXD) | Funktion | |--------------|----------| | lxc launch | Erstellen+Starten | | lxc exec | Befehl ausführen | | lxc snapshot | Snapshot | | lxc file push/pull | Dateien | | lxc config | Konfiguration |

| Datei | Funktion | |-------|----------| | /var/lib/lxc/*/config | Container-Config | | /etc/lxc/default.conf | Standard-Config | | /etc/subuid | UID-Mapping | | /etc/subgid | GID-Mapping |

Fazit

LXC bietet VM-ähnliche Isolation mit Container-Performance. Für traditionelle Server-Workloads ist LXC oft besser geeignet als Docker. LXD vereinfacht die Verwaltung erheblich. Proxmox nutzt LXC für seine Container-Virtualisierung. Unprivileged Container erhöhen die Sicherheit durch UID-Mapping. Für Microservices bleibt Docker die bessere Wahl.