Software Virtualisation with LXD in Linux for Private Hosting via Cloudflare

Use LXD to run virtual linux images more compact than Docker and Oracle Virtualbox

In our project we will host a nextcloud service for a private dropbox like service using cloudflare tunnel (Other alternatives using ngrok etc.)

First install LXD

sudo apt install lxd lxd-client

Message during the end of LXD installation

LXD has been installed. You must run `sudo lxd init` to
perform the initial configuration of LXD.
Be sure to add user(s) to the lxd group, then have them
logout and back in to properly setup their access.

If you want to see what images areĀ  available (like a specific architecture or a “cloud” version), use the list command:

Bash

lxc image list images: 
lxc image list images: | grep -i "debian"

The launch command both downloads the image and starts the container in one go.

Bash

lxc launch images:debian/13 my-sandbox

Breaking down the name:

  • images:: The remote server where LXD looks for templates.

  • debian/13: The distribution and version (you can also use ubuntu/22.04, alpine/3.18, etc.).

  • my-sandbox: The name you choose for your container.

Summary of Lifecycle Commands

Action Command
Start lxc start my-sandbox
Stop lxc stop my-sandbox
Restart lxc restart my-sandbox
List all lxc list
Delete lxc delete my-sandbox --force

Resource Limiting (Optional but Recommended)

Since you are likely working on a headless setup where resources matter, you can limit the container’s footprint immediately after creation:

Bash

# Limit to 512MB of RAM
lxc config set my-sandbox limits.memory 512MB

# Limit to 1 CPU core
lxc config set my-sandbox limits.cpu 1

Copying Files

To move files between your host machine and the sandbox:

# Host to Container
lxc file push ./index.html my-sandbox/var/www/html/

# Container to Host
lxc file pull my-sandbox/etc/apache2/apache2.conf ./backup-configs/

Once the container is launched, it is running “detached” in the background. Use these commands to manage it:

Execute a Command

To run a command inside the container without entering it:

Bash

lxc exec my-sandbox -- apt update

Get an Interactive Shell

This is like “SSHing” into your sandbox. You will get a root prompt inside the container:

lxc exec my-sandbox -- bash

Network Errors
i can ping from my-sandbox to google 8.8.8.8 but unable to update apt

The Quick Fix: Manually Set DNS

The fastest way to test this is to force a DNS server inside the container.

  1. Enter the container:

  2. Edit the resolv.conf file:
echo "nameserver 8.8.8.8" > /etc/resolv.conf

3. The Permanent Fix (LXD Level)

Editing /etc/resolv.conf manually is often overwritten by the system. To fix this permanently for your sandbox, you should tell the LXD bridge to provide a specific DNS server via DHCP.

Run this on your host machine:

lxc network set lxdbr0 ipv4.address 10.0.3.1/24
lxc network set lxdbr0 ipv4.nat true
lxc network set lxdbr0 dns.domain sandbox
lxc network set lxdbr0 raw.dnsmasq "dhcp-option=6,8.8.8.8,8.8.4.4"

Note: Replace lxdbr0 if you named your bridge something else during lxd init.

 

Leave a Reply

Your email address will not be published. Required fields are marked *