Building LXD images using Packer.
- 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
Install LXD from Snap.
sudo snap install lxdAdd your account to lxd gorup.
sudo usermod -aG lxd $USER
newgrp lxd
# Checks your groups
groupsInitialize the LXD service using default configuration.
lxd init --autoCreate a profile predefined for testing purpose.
lxc profile create develop < develop-profile.yaml
# Checks the created profile
lxc profile listAdd 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/nullInstall Packer from the newly added repository.
sudo apt-get update
sudo apt-get install packerAnd the following command adds an LXD plugin under your home directory.
cd <path/to/image/directory>
packer init .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 listThe built images can be run with the following command.
lxc launch <image-alias> <container-name> -p developWith 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>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-ngexport LXD_APT_PROXY=http://_gateway:3142
# Subsequent builds may get performance gain- The cached packages are persisted in the local
/var/cache/apt-cacher-ngdirectory. - The packages fetched over HTTPS are not cached, and will be always downloaded directly from the remote repositories.