LXC/LXD Command Cheatsheet

From: Getting started with LXD Containerization (Full Guide!)


First things first, how to get out of trouble

lxc stop container-name

lxc delete container-name


create a container

Which remote servers (repositories) are available for image download

lxc remote list

List the images available on a remote server (repository)

lxc image list images: or lxc image alias list images:

"images" is the remote server (repository).

Search for keywords in the list of images available on a remote server (repository)

lxc image list images: ubuntu focal

"ubuntu" and "focal" are keywords.

List the containers currently running

lxc list customize command lxc list -c n,s,4,image.description:image lxc list -c n,s,4,6,image.description:image Note: 6 displays ipv6 addresses

Create/Run (launch) a container

lxc launch images:ubuntu/focal ubuntu

"images" is the repository we want to pull an image from. "ubuntu/focal" is the image to be pulled. "ubuntu" is the name of the container we are creating.

Notes:

lxc info container-name

Execute a command inside a container

lxc exec ubuntu -- apt install apache2

"ubuntu" is the container name. "apt install apache2" is the command to run inside the container

Now we should have apache2 running inside the container.


miscellaneous

Start a container

lxc start container-name

Stop a container

lxc stop container-name

Restart a container

lxc restart container-name

OS system identification data for the container

lxc exec container-name -- cat /etc/os-release

It is a standard file in systemd-based distributions. This file is used to reliably determine the OS version and other information.


Snapshot

Take a snapshot

lxc snapshot container-name snapshot-name

View container and snapshot data

lxc info container-name

Delete a snapshot

lxc delete container-name/snapshot-name

Restore a container with a snapshot

lxc restore container-name snapshot-name

Start container when system starts

lxc config set container-name boot.autostart 1

Help prevent a container using all of the host system's memory

lxc config set container-name limits.memory 1GB

Show a container's configuration

lxc config show container-name

Run a shell in a container

lxc exec container-name -- bash

Enter "exit" to exit the bash shell.


Profile

Show Profile

lxc profile show profile-name

get Profile Value

lxc profile get profile-name key

Set Profile Value

lxc profile set profile-name key value

Create Profile

lxc profile create profile-name

Delete Profile

lxc profile delete profile-name

Edit Profile

lxc profile profile-name