LXD¶
Differences Between Docker and Linux Containers¶
Docker and Linux Containers (such as LXC) both use the same underlying Linux technologies i.e., chroot, cgroups, namespaces, etc. They differ in purpose: Docker focuses on lightweight application containers (typically for microservices), while Linux Containers is intended for full operating system containers.
Docker containers usually run a single process. In contrast, LXC containers behave more like virtual machines, offering a full OS environment.
Originally, Docker used LXC under the hood, but starting with version 0.9, it introduced its own runtime: libcontainer.
Docker emphasized application packaging, automation, and portability: leveraging Dockerfiles, layered filesystems, and compliance with the OCI standard. LXC, on the other hand, targets full system environments.
Linuxcontainers.org project provides LXD, a powerful tool for managing both Linux Containers and virtual machines, offering a CLI experience similar to Docker.
Personally, I prefer LXD for creating virtual machines in development due to its flexibility and Docker-like interface.
Commands¶
Below we will discuss commands for managing VMs. Same commands will apply for Linux Containers.
Initialization¶
Initialize LXD for first time use:
Images & Remote Image Servers¶
List remote repository/servers available to pull images.
List images from a particular remote repository
Search images using keyword MATCH from a remote repository
Copy an image from remote repository.
lxc image copy REPOSITORY_NAME:IMAGE_ALIAS_OR_FINGERPRINT local: --alias ubuntu-focal
# lxc image copy ubuntu-minimal:52dd513b467a local: --alias ubuntu24
# lxc image copy ubuntu-minimal:ec0518e181c5 local: --alias ubuntu22
Now, List images locally
Delete a local image:
(Also checkout lxc image export and lxc image import)
Container & VM Management¶
List all containers/virtual machine
Launch/run a virtual machine (use virtual machine image type)
Open shell in a virtual machine
Open console in a virtual machine
Stop a virtual machine
Start a virtual machine
Restart a virtual machine
Delete a virtual machine
Copy an instance to create a new instance.
Migrate or move an instance to different name/location
Configure VM/Containers¶
Set limit for memory of a container/virtual machine
Set limit for cpu for a container/virtual machine
Set CPU Pinning for a container/virtual machine
Show all config for a container/virtual machine
Edit config for a container/virtual machine
List devices attached to a container:
Remove a device:
Profiles¶
A profile contains config, storage volumes, network interfaces details. They are templates to spin-up new instances. They are alternative to manually modifing config for each instance. List profiles
Get details of a profile
Copy a profile
Use profile to launch an instance launch/run a container/virtal machine
Edit a profile
Copying Files with Host¶
COPY a file from host to instance
COPY a file from instance to host
Mount a directory from host to instance
lxc config device add CONTAINER_NAME NEW_DEVICE_NAME disk source=PATH_ON_HOST(/home/ubuntu) path=TARGET_PATH_IN_CONTAINER
LXD inside LXD¶
For running lxd inside lxd, make sure following settings are configured to the parent lxd instance
Snapshots¶
Create a snapshot of a container
Get details/status of a container; this also shows all snapshots that container has.
Delete a snapshot of a container
Restore a snapshot, i.e. rollback container to previous state
Network Management¶
List Networks
Show network details
Create a new bridge network
Attach container/virtual machine to a network
Attach container/virtual machine from a network
DNS to ping between containers/virtual machines. Service discovery similar to docker and k8s:
Storage Management¶
List storage pools:
Show details of a storage pool:
Create a new storage pool:
Delete a storage pool:
Cloud-init¶
Launch container/virtual machine with cloud-init
Next, Verify cloud-init:lxc exec VM_NAME bash
# 1. Check cloud-init status
$ cloud-init status --wait
# 2. Fetch cloud-init data
$ cloud-init query userdata
# 3. Validate cloud-init schema
$ cloud-init schema --system --annotate
# 4. Verify changes were applied