Share host folder with multiple containers

Is it possible to share the same host folder with multiple containers? (read only is fine).

When I do:

lxc config device add container1 ct1-aaHost disk source=/aa/host/ path=/aa/host/
lxc config device add container2 ct2-aaHost disk source=/aa/host/ path=/aa/host/

The second statement gives the error: More than one disk device uses the same path “/aa/host/”

Any help would be appreciated.

stgraber@castiana:~$ lxc launch images:ubuntu/22.04 u1
Creating u1
Starting u1                                 
stgraber@castiana:~$ lxc launch images:ubuntu/22.04 u2
Creating u2
Starting u2
stgraber@castiana:~$ lxc config device add u1 home disk source=/home path=/mnt/home
Device home added to u1
stgraber@castiana:~$ lxc config device add u2 home disk source=/home path=/mnt/home
Device home added to u2
stgraber@castiana:~$ lxc version
Client version: 5.13
Server version: 5.13
stgraber@castiana:~$ lxc list u
+------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
| NAME |  STATE  |         IPV4          |                     IPV6                      |   TYPE    | SNAPSHOTS |
+------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
| u1   | RUNNING | 10.128.192.113 (eth0) | fd42:ae5f:98ab:a816:216:3eff:fe38:fc26 (eth0) | CONTAINER | 0         |
+------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
| u2   | RUNNING | 10.128.192.104 (eth0) | fd42:ae5f:98ab:a816:216:3eff:fe52:d2a4 (eth0) | CONTAINER | 0         |
+------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
stgraber@castiana:~$ 

So the error means that container2 has two disk devices that both have the same path value.
You should see that pretty clearly by looking at the current config through lxc config show --expanded container2

I use a profile to share folders, like this:

sudo mkdir -p /aa/host
lxc profile create aa
lxc profile device add aa aa disk source=/aa/host path=/aa/host readonly=on
lxc profile add container1 aa
lxc profile add container2 aa

To see which containers use this profile, I do this:

lxc profile show aa

config: {}
description: ""
devices:
  aa:
    path: /aa/host
    readonly: "on"
    source: /aa/host
    type: disk
name: aa
used_by:
- /1.0/instances/container1
- /1.0/instances/container2

If I want the folder to be writeable by the containers, I set the permissions on the host:
sudo chown -R 1000000:1000000 /aa/host

Once I have such a profile, I create similar profiles by copying it and editing it:

lxc profile copy aa bb
lxc profile edit bb

I find this easier than looking up the usage of the “lxc [profile] device” command.

1 Like