LXC

It’s surprising that people almost never think about LXC containers when they think about linux containers. Lets talk about them for a change.

This document is not meant to be a step by step tutorial but more of a quick reference.

LXC/LXD?

LXC/LXD is an OS container managemet tool from Ubuntu. Its nothing new, in fact its production ready with LTS releases coming with 5 years of security and bugfix updates.

Install on Ubuntu

LXC/LXD deb packages in the repository are pretty old (are LTS versions) but its recommended to use the latest versions which are available only as snap packages. So its recommended to install the snap package.

For the latest stable release, use:

snap install lxd

If you previously had the LXD deb package installed, you can migrate all your existing data over with:

lxd.migrate

Initial configuration

Before you can create containers, you need to tell LXD a little bit about your storage and network needs.

This is all done with:

lxd init

List local images available

lxc image list

List remote images

To list all images avaialble (list can be very long)

lxc image list images: | less

Search images

lxc image list images: '<search term>'

Launch container

Ubuntu:

lxc launch ubuntu:18.04 c1

where c1 is the name of the container

Fedora:

lxc launch images:fedora/31 c2

Launch a terminal inside a container

Root shell

lxc exec c1 -- bash

Where c1 is the container name.

Non-root shell

There are multiple ways to do this, but here’s one for an ubuntu container

lxc exec c1 -- sudo su - ubuntu

It will execute the command in the container and display the output

Run any command:

lxc exec c1 -- whoami

It will execute the command in the container and display the output

Expose ports

Note that LXD 3.0.x only supports TCP to TCP proxy devices. Support for UDP was added in later versions.

TCP

lxc config device add c1 http-port proxy listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80
lxc config device add c1 https-port proxy listen=tcp:0.0.0.0:443 connect=tcp:127.0.0.1:443

UDP

lxc config device add c1 udp-port proxy listen=udp:0.0.0.0:13359 connect=udp:127.0.0.1:13359

Stop container

lxc stop c1

Delete container

lxc delete c1

References