Managing LXD containers
We have installed LXD and deployed our first container with it. In this recipe, we will learn various LXD commands that manage the container lifecycle.
Getting ready…
Make sure that you have followed the previous recipes and created your first container.
How to do it…
Follow these steps to manage LXD containers:
- Before we start with container management, we will need a running container. If you have been following the previous recipes, you should already have a brand new container running on your system. If your container is not already running, you can start it with the
lxc start
command:$ lxc start c1
- To check the current state of a container, use
lxc list
, as follows:$ lxc list c1
This command should list only containers that have
c1
in their name. - You can also set the container to start automatically. Set the
boot.autostart
configuration option totrue
and your container will start automatically on system boot. Additionally, you can specify a delay before autostart and a priority in the autostart list:$ lxc config set c1 boot.autostart true
- Once your container is running, you can open a bash session inside a container using the
lxc exec
command:$ lxc exec c1 -- bash root@c1:~# hostname c1
This should give you a root shell inside a container. Note that to use bash, your container image should have a bash shell installed in it. With alpine containers, you need to use
sh
as the shell as alpine does not contain the bash shell. - LXD provides the option to pause a container when it's not being actively used. A paused container will still hold memory and other resources assigned to it, but not receive any CPU cycles:
$ lxc pause c1
- Containers that are paused can be started again with
lxc start
. - You can also restart a container with the
lxc restart
command, with the option to perform a stateful or stateless restart:$ lxc restart --stateless c1
- Once you are done working with the container, you can stop it with the
lxc stop
command. This will release all resources attached to that container:$ lxc stop c1
At this point, if your container is an ephemeral container, it will be deleted automatically.
- If the container is no longer required, you can explicitly delete it with the
lxc delete
command:$ lxc delete c1
There's more…
For those who do not like to work with command line tools, you can use a web-based management console known as LXD GUI. This package is still in beta but can be used on your local LXD deployments. It is available on GitHub at https://github.com/dobin/lxd-webgui.
See also
- Get more details about LXD at https://www.stgraber.org/2016/03/19/lxd-2-0-your-first-lxd-container-312/
- LXC web panel: https://lxc-webpanel.github.io/install.html