Skip to content

openclosed-dev/packer-lxd-examples

Repository files navigation

packer-lxd-examples

Building LXD images using Packer.

Prerequisites

  • Ubuntu 24.04 LTS (may run on WSL)
  • LXD 5.21.3 LTS or higher
  • Packer 1.12.0 or higher
  • Packer LXD plugin 1.0.2 or higher
  • (optional) APT-Cacher NG

Installing LXD

Install LXD from Snap.

sudo snap install lxd

Add your account to lxd gorup.

sudo usermod -aG lxd $USER
newgrp lxd
# Checks your groups
groups

Initialize the LXD service using default configuration.

lxd init --auto

Create a profile predefined for testing purpose.

lxc profile create develop < develop-profile.yaml
# Checks the created profile
lxc profile list

Installing Packer

Add an apt repository managed by HashiCorp.

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo tee /etc/apt/keyrings/hashicorp.asc > /dev/null
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/hashicorp.asc] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list > /dev/null

Install Packer from the newly added repository.

sudo apt-get update
sudo apt-get install packer

And the following command adds an LXD plugin under your home directory.

cd <path/to/image/directory>
packer init .

Building LXD images

Delete the image if already exists.

lxd image delete <image-alias>

Invoke the following command.

cd <path/to/image/directory>
packer build .

Images built are registered in the local image cache.

lxc image list

Running LXD containers

The built images can be run with the following command.

lxc launch <image-alias> <container-name> -p develop

With develop profile specified, the containers equipped with GUI accept RDP connection. The target IP address is that of the Ubuntu machine hosting the container.

The login credentials provided by these examples are user1/secret, which must be changed in production environment.

The password hash is generated with the following command and specified with hashed_passwd property in the profile.

openssl passwd -6 <password>

Advanced settings

Caching apt packages for faster build

Install apt-cacher-ng for locally caching the packages downloaded from the apt repositories over HTTP protocol.

# Installs apt-cacher-ng 
sudo apt install apt-cacher-ng
# Checks the service is up and running
systemctl status apt-cacher-ng
export LXD_APT_PROXY=http://_gateway:3142
# Subsequent builds may get performance gain
  • The cached packages are persisted in the local /var/cache/apt-cacher-ng directory.
  • The packages fetched over HTTPS are not cached, and will be always downloaded directly from the remote repositories.

About

Building LXD images using Packer

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published