Category : Cheat Sheets
Here are some of the most used commands for creating and managing an LXC/ LXD host and containers. It’s assumed that you have a working environment and a privileged SSH connection to the LXC server for issuing the commands.
Start and Stop a LXC Container
Starting out with the basics here – starting and stopping an LXC container.
lxc start [CONTAINER] lxc stop [CONTAINER]
Display a list of container details for started and stopped containers. The name field is what’s usually used in other commands to reference the specific container.
Create Container from Image
There are further details below on managing images and remote image repository, which you’ll need when creating a new container.
This example will create a new container and start it using the Ubuntu 1604 template. Change [CONTAINER] to be the name of the new container.
lxc launch ubuntu:16.04 [CONTAINER]
Removing a container cannot be undone – beware!
lxc delete [CONTAINER]
Linux Containers are created from templates or images that are stored locally or downloaded from remote servers.
List Image Repositories
Local and Remote LXC servers and remote image servers can be added to your LXC installation and can be used to download images from when required. Run the below command to see what sources you have.
lxc remote list
List available images
Images that have been downloaded, imported or cached are stored locally in the image repository. The output will list the image name, size and various other details.
lxc image list
Remote images that reside on an image repository or remote LXC server can also be listed. This is great for seeing what images are available when creating new containers. Change [REMOTE_NAME] to be the name of the image repository from the image list command. Note: you’ll need to keep the : symbol at the end.
lxc image list [REMOTE_NAME]:
Get image details
Further details can be obtained from an image file than what’s displayed with image list. The below command will detail all information known about the image. Replace [IMAGE_NAME] with a valid image name displayed in the image list command, such as ubuntu-xenial.
lxc image info [IMAGE_NAME]
Add a new Image Repository
There are various public image repositories that can be added to your LXC installation. LinuxContainers.org is a common one and hosts several distribution types. Replace [NAME] with the text name you’d like to give to the repository (it’s just an alias) and [HOST] with the address of the repository.
lxc remote add [NAME] [HOST]
lxc remote add lxc-org images.linuxcontainers.org
Delete a local image
Replace [IMAGE_NAME] with the the alias or fingerprint of the image.
lxc image delete [IMAGE_NAME]
Create new Image from Running Container
You can create a new image from an existing container with a simple command however it’s important to ensure that the created template will contain everything that the running container contained – such as SSH keys, data, etc. It’s therefore important to ensure you clean up anything which may be sensitve before running this command.
lxc publish [CONTAINER] --alias [ALIAS]
You’ll need to change [CONTAINER] to your Linux container name and [ALIAS] to the name you’d like to use for your new image.
All the below instructions will assume you’re referring to a container alias called [CONTAINER]. You’ll need to replace this, wherever it’s seen, with the name of the Linux Container you’re acting on.
And config command using set can be altered to use get to retrieve what the current setting is. If the get returns nothing then it means it has not been manually set and the default value will be used.
Auto Start Container
Set the container to start automatically when the LXC service starts – usually at host boot time. Use 1 to enable and 0 to disable.
lxc config set [CONTAINER] boot.autostart 1
You can also use boot.autostart.delay to set a delay in seconds after starting this container, before starting the next.
lxc config set [CONTAINER] boot.autostart.delay 30
Start up can be ordered using lxc.autostart.order to prioritise which containers are started first. Higher numbers are started first.
lxc config set [CONTAINER1] boot.autostart.order 10 lxc config set [CONTAINER2] boot.autostart.order 8
See CPU Resource Limits for more information on constraining CPU resources.