Playing around with LXD containers (LXC) on Ubuntu

We have set up LXD on either our personal computer or on the cloud (like DigitalOcean and Scaleway). Actually, we can even try LXD online for free at https://linuxcontainers.org/lxd/try-it/

What shall we do next?

Commands through “lxc”

Below we see a series of commands that start with lxc, then we add an action and finally we add any parameters. lxc here is the program that does the communication with the LXD service and performs the actions that we request. That is,

lxc action parameters

There are also a series of commands that are specific to a type of object. In that case, we add in the the object type and continue with the action and the parameters.

lxc object action parameters

List the available containers

Let’s use the list action, which lists the available containers.

ubuntu@myvps:~$ lxc list
Generating a client certificate. This may take a minute...
If this is your first time using LXD, you should also run: sudo lxd init
To start your first container, try: lxc launch ubuntu:16.04
+------+-------+------+------+------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+------+-------+------+------+------+-----------+
ubuntu@myvps:~$
The first time you run lxc list, it creates a client certificate (installs it in ~/.config/lxc/). It takes a few seconds and this process takes place only once.
The command also advices us to run sudo lxd init (note: lxd) if we haven’t done so before. Consult the configuration posts if in doubt here.
In addition, this command also suggests us on how to start (launch) our first container.
Finally, it shows the list of available containers on this computer, which is empty (because we have not created any yet).

List the locally available images for containers

Let’s use the image object, and then the list action, which lists the available (probably cached) images that are hosted by our LXD service.

ubuntu@myvps:~$ lxc image list
+-------+-------------+--------+-------------+------+------+-------------+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
+-------+-------------+--------+-------------+------+------+-------------+
ubuntu@myvps:~$

There are no locally available images yet, so the list is empty.

List the remotely available images for containers

Let’s use the image object, and then the list action, and finally a remote repository specifier (ubuntu:) in order to list some publicly available images that we can use to create containers.
ubuntu@myvps:~$ lxc image list ubuntu:
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+
|       ALIAS        | FINGERPRINT  | PUBLIC |                   DESCRIPTION                   |  ARCH   |   SIZE   |          UPLOAD DATE          |        
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+        
| p (5 more)         | 6b6fa83dacb0 | yes    | ubuntu 12.04 LTS amd64 (release) (20160627)     | x86_64  | 155.43MB | Jun 27, 2016 at 12:00am (UTC) |        
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+        
| p/armhf (2 more)   | 06604b173b99 | yes    | ubuntu 12.04 LTS armhf (release) (20160627)     | armv7l  | 135.90MB | Jun 27, 2016 at 12:00am (UTC) |        
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+    
...
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+        
| x (5 more)         | f452cda3bccb | yes    | ubuntu 16.04 LTS amd64 (release) (20160627)     | x86_64  | 138.23MB | Jun 27, 2016 at 12:00am (UTC) |        
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+        
| x/arm64 (2 more)   | 46b365e258a0 | yes    | ubuntu 16.04 LTS arm64 (release) (20160627)     | aarch64 | 146.72MB | Jun 27, 2016 at 12:00am (UTC) |        
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+        
| x/armhf (2 more)   | 22f668affe3d | yes    | ubuntu 16.04 LTS armhf (release) (20160627)     | armv7l  | 148.18MB | Jun 27, 2016 at 12:00am (UTC) |        
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+        
...
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+        
|                    | 4c6f7b94e46a | yes    | ubuntu 16.04 LTS s390x (release) (20160516.1)   | s390x   | 131.07MB | May 16, 2016 at 12:00am (UTC) |        
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+        
|                    | ddfa8f2d4cfb | yes    | ubuntu 16.04 LTS s390x (release) (20160610)     | s390x   | 131.41MB | Jun 10, 2016 at 12:00am (UTC) |        
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+        
ubuntu@myvps:~$
The repository ubuntu: is a curated list of containers from Canonical, and has all sorts of Ubuntu versions (from 12.04 or newer) and architectures (like x86_64, ARM and even S390x).
The first column is the nickname or alias. Ubuntu 16.04 LTS for x86_64 has the alias x, so we can use that or we can specify the fingerprint (here: f452cda3bccb).

Show information for a remotely available image for containers

Let’s use the image object, and then the list action, and finally a remote image specifier (ubuntu:x) in order to get info out of a specific publicly available image that we can use to create containers.
ubuntu@myvps:~$ lxc image info ubuntu:x
    Uploaded: 2016/06/27 00:00 UTC                                                                                                                           
    Expires: 2021/04/21 00:00 UTC                                                                                                                            

Properties:                                                                                                                                                  
    aliases: 16.04,x,xenial                                                                                                                                  
    os: ubuntu                                                                                                                                               
    release: xenial                                                                                                                                          
    version: 16.04                                                                                                                                           
    architecture: amd64                                                                                                                                      
    label: release                                                                                                                                           
    serial: 20160627                                                                                                                                         
    description: ubuntu 16.04 LTS amd64 (release) (20160627)                                                                                                 

Aliases:                                                                                                                                                     
    - 16.04                                                                                                                                                  
    - 16.04/amd64                                                                                                                                            
    - x                                                                                                                                                      
    - x/amd64                                                                                                                                                
    - xenial                                                                                                                                                 
    - xenial/amd64                                                                                                                                           

Auto update: disabled           
ubuntu@myvps:~$

Here we can see the full list of aliases for the 16.04 image (x86_64). The simplest of all, is x.

Life cycle of a container

Here is the life cycle of a container. First you initialize the image, thus creating the (stopped) container. Then you can start and stop it. Finally, in the stopped state, you may select to delete it.

LifecycleLXD

 We initialise a container with Ubuntu 16.04 (ubuntu:x) and give the name mycontainer. Since we do not have yet any locally cached images, this one is downloaded and cached for us. If we need another container with Ubuntu 16.04, it will be prepared instantly since it is already cached localy.
When we initialise a container from an image, it gets the STOPPED state. When we start it, it gets into the RUNNING state.
When we start a container, the runtime (or rootfs) is booted up and may take a few seconds until the network is up and running. Below we can see that it took a few seconds until the container managed to get the IPv4 IP address through DHCP from LXD.
We can install web servers and other services into the container. Here, we just execute a BASH shell in order to get shell access inside the container and run the uname command.
We promptly exit from the container and stop it.
Then, we delete the container and verify that it has been delete (it is not shown in lxc list).
Finally, we also verify that the image is still cached locally on LXD, waiting for the next creation of a container.
Here are the commands,
ubuntu@myvps:~$ lxc init ubuntu:x mycontainer
Creating mycontainer                                                                                                                                         
Retrieving image: 100%                                                                                                                                       
ubuntu@myvps:~$ lxc image list
+-------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+                           
| ALIAS | FINGERPRINT  | PUBLIC |                 DESCRIPTION                 |  ARCH  |   SIZE   |         UPLOAD DATE          |                           
+-------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+                           
|       | f452cda3bccb | no     | ubuntu 16.04 LTS amd64 (release) (20160627) | x86_64 | 138.23MB | Jul 22, 2016 at 2:10pm (UTC) |                           
+-------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+
ubuntu@myvps:~$ lxc list
+-------------+---------+------+------+------------+-----------+                                                                                             
|    NAME     |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |                                                                                             
+-------------+---------+------+------+------------+-----------+                                                                                             
| mycontainer | STOPPED |      |      | PERSISTENT | 0         |                                                                                             
+-------------+---------+------+------+------------+-----------+                                                                                             
ubuntu@myvps:~$ lxc start mycontainer
ubuntu@myvps:~$ lxc list     
+-------------+---------+------+-----------------------------------------------+------------+-----------+                                                    
|    NAME     |  STATE  | IPV4 |                     IPV6                      |    TYPE    | SNAPSHOTS |                                                    
+-------------+---------+------+-----------------------------------------------+------------+-----------+                                                    
| mycontainer | RUNNING |      | 2607:f2c0:f00f:2770:216:3eff:fe4a:ccfd (eth0) | PERSISTENT | 0         |                                                    
+-------------+---------+------+-----------------------------------------------+------------+-----------+
ubuntu@myvps:~$ lxc list
+-------------+---------+-----------------------+-----------------------------------------------+------------+-----------+                                   
|    NAME     |  STATE  |         IPV4          |                     IPV6                      |    TYPE    | SNAPSHOTS |                                   
+-------------+---------+-----------------------+-----------------------------------------------+------------+-----------+                                   
| mycontainer | RUNNING | 10.200.214.147 (eth0) | 2607:f2c0:f00f:2770:216:3eff:fe4a:ccfd (eth0) | PERSISTENT | 0         |                                   
+-------------+---------+-----------------------+-----------------------------------------------+------------+-----------+                                   
ubuntu@myvps:~$ lxc exec mycontainer -- /bin/bash       
root@mycontainer:~# uname -a
Linux mycontainer 4.4.0-31-generic #50~14.04.1-Ubuntu SMP Wed Jul 13 01:07:32 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux                                        
root@mycontainer:~# exit
exit
ubuntu@myvps:~$ lxc list
+-------------+---------+-----------------------+-----------------------------------------------+------------+-----------+                                   
|    NAME     |  STATE  |         IPV4          |                     IPV6                      |    TYPE    | SNAPSHOTS |                                   
+-------------+---------+-----------------------+-----------------------------------------------+------------+-----------+                                   
| mycontainer | RUNNING | 10.200.214.147 (eth0) | 2607:f2c0:f00f:2770:216:3eff:fe4a:ccfd (eth0) | PERSISTENT | 0         |                                   
+-------------+---------+-----------------------+-----------------------------------------------+------------+-----------+                                   
ubuntu@myvps:~$ lxc stop mycontainer
ubuntu@myvps:~$ lxc list
+-------------+---------+------+------+------------+-----------+                                                                                             
|    NAME     |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |                                                                                             
+-------------+---------+------+------+------------+-----------+                                                                                             
| mycontainer | STOPPED |      |      | PERSISTENT | 0         |                                                                                             
+-------------+---------+------+------+------------+-----------+       
ubuntu@myvps:~$ lxc delete mycontainer
ubuntu@myvps:~$ lxc list
+------+-------+------+------+------+-----------+                                                                                                            
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |                                                                                                            
+------+-------+------+------+------+-----------+                                                                                                            
ubuntu@myvps:~$ lxc image list
+-------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+                           
| ALIAS | FINGERPRINT  | PUBLIC |                 DESCRIPTION                 |  ARCH  |   SIZE   |         UPLOAD DATE          |                           
+-------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+                           
|       | f452cda3bccb | no     | ubuntu 16.04 LTS amd64 (release) (20160627) | x86_64 | 138.23MB | Jul 22, 2016 at 2:10pm (UTC) |                           
+-------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+                           
ubuntu@myvps:~$

Some tutorials mention the launch action, which does both init and start. Here is how the command would have looked like,

lxc launch ubuntu:x mycontainer

We are nearing the point where we can start doing interesting things with containers. Let’s see the next blog post!

Permanent link to this article: https://blog.simos.info/playing-around-with-lxd-containers-lxc-on-ubuntu/

1 comment

1 ping

    • Chris on July 24, 2016 at 07:39
    • Reply

    Nice, thanks for the overview.

  1. […] posts we saw how to set up LXD on a DigitalOcean VPS, how to set up LXD on a Scaleway VPS, and how the lifecycle of an LXD container looks […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.