How to add or mount directory in LXD (Linux container)

In LXD, you can add or mount a directory from the host system to a container using the lxc config device command. Here are the steps to add or mount a directory in a container:

  1. Open a terminal window and log in as the root user or use the sudo command to run the following commands.
  2. Get the name of the container you want to add the directory to by using the lxc list command.
  3. Use the lxc config device add command to add a new device to the container. The command takes the format:
lxc config device add <container-name> <device-name> disk source=<path-on-host> path=<path-in-container>

For example, to add the directory /mydata on the host to the container named mycontainer and mount it as /data in the container:

lxc config device add mycontainer mydata disk source=/mydata path=/data
  1. To check if the directory is successfully added and mounted, you can use the lxc config show command followed by the container name.
lxc config show mycontainer
  1. To remove the added directory from the container, use the lxc config device remove command followed by the container name and device name
lxc config device remove mycontainer mydata

You should now have the directory from the host system mounted in the specified path in the container. You can now access the files in the mounted directory from inside the container as if they were native to the container. Please keep in mind that the source directory on the host must exist before creating the mountpoint in the container.

(www.almostthererescue.org)

Leave a Comment