Nesting in LXD Containers

LXD containers are much more efficient than Virtual Machines in not requiring a full installation, they have nearly instant boot times, require less storage space, and less memory. You can nest docker inside of LXD and offer one or more docker containers in a single LXD container. Nesting provides an easy way to give a Docker container a dedicated address without a lot of steps.

Create an untagged profile for your main LAN on your LXD host.

lxc profile create untagged

Check your interfaces on your LXD host to see which parent device you need to use and make the settings on the untagged profile. Your parent device will be something other than qvs0.

ip route
lxc profile device add untagged eth0 nic nictype=macvlan parent=qvs0 

Add a descriptive comment to the untagged profile for documentation purposes by editing it.

lxc profile edit untagged

You will be in the “vi” editor. Move your cursor into the description quotes and press “i” to go into insert mode. Once you are finished editing, do an “ESC :wq” to write out the file and quit the vi editor.

List the profiles on the LXD host.

lxc profile list

Create the LXD container using the new untagged profile.

lxc launch ubuntu:22.04 demo --profile default --profile untagged -c boot.autostart=true -c security.nesting=true -c limits.memory=2048MB -c limits.cpu.allowance=20%

Connect to the container console root account.

lxc exec demo bash

Perform the following commands to update the container.

apt update && apt upgrade -y
adduser scott
snap install docker
addgroup --system docker
usermod -aG docker scott
snap disable docker
snap enable docker
apt install nano
apt install net-tools
apt install neofetch
apt remove --purge openssh-server
apt install openssh-server

Now exit the container.

exit

Next we will publish a container image that can be used to create future containers using our work as a starting point.

lxc stop demo
lxc image list
lxc publish demo --alias Ubuntu-22.04-Docker description="Ubuntu 22.04 with Docker & Scott Account"

Now that we have a published image, we can create a new container using that image as a template.

lxc launch Ubuntu-22.04-Docker demo2 --profile default --profile untagged -c boot.autostart=true -c security.nesting=true -c limits.memory=2048MB -c limits.cpu.allowance=20%

List your images.

lxc image list

Restart the demo container and list the containers.

lxc start demo
lxc  list

If your new container lacks a “docker” device and only lists “eth0”, you need to change the container to “privileged”.

lxc config set demo security.privileged true
lxc restart demo

Connect to the container via ssh (your username and ip address will differ).

ssh scott@172.16.1.65

Create the sample docker application as in the video.

mkdir drawio
cd drawio
nano docker-compose.yml

Add the following text into the file.

version: '3.5'
services:
  drawio:
    image: fjudith/draw.io
    container_name: drawio
    restart: unless-stopped
    ports:
      - 80:8080
      - 443:8443

Save the file with a CTRL X and follow the prompts to update the file and exit the nano editor.

Now execute the YML file.

docker-compose up -d

Go to your web browser and type in the address of the LXD host and the drawio application should come up since it is set to display on the default port 80.