LXD Containers Mount Host Folders

Docker containers have to mount folders on the docker host to have persistent data. LXD containers have a read/write file system unlike Docker so accessing host files is normally not necessary.

In this video, I show how to mount host folders from a Docker container and make them read/write.

First I create a simple container:

lxc launch ubuntu:20.04 test --profile default -c boot.autostart=true

Connect to the container and create a user account and also a mount point folder:

lxc exec test bash
adduser scott
su - scott
mkdir hostdata
exit
exit

I created a folder on the LXD host that I wanted to share:

mkdir  /home/scott/Desktop/mydesk

Still on the LXD host, I added the connection for the host folder “mydesk” to the LXD mount point folder “hostdata”:

lxc config device add test mydisk disk source=/home/scott/Desktop/mydesk path=/home/scott/hostdata

At this point, files can be accessed on the host, but the LXD user has no privilege to add or delete files since the access is read-only.

To make the access read/write, you must first add an entry to the subordinate GID file on the LXD host:

cat /etc/subgid
id

Once you have the UID, you add it to the subgid file. This is done only once on the LXD host and will allow remapping for all LXD containers on the LXD host to the UID 1000 user:

echo "root:1000:1" | sudo tee -a /etc/subuid /etc/subgid

To remap the host ID inside the container:

lxc exec test bash
id scott
exit

Then on the LXD host, use the LXD host ID (1000) for scott and the LXD container ID (1001) for scott:

lxc config set test raw.idmap "both 1000 1001"
lxc restart test

Now the mounted folder will be read/write from the LXD user account scott.

I also demonstrated a use case for LXD Dashboard whereby /var/lxdware/backups could be mapped to the LXD host to allow LXD container backups to be stored on the LXD host.