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:
- Open a terminal window and log in as the root user or use the
sudocommand to run the following commands. - Get the name of the container you want to add the directory to by using the
lxc listcommand. - Use the
lxc config device addcommand 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
- To check if the directory is successfully added and mounted, you can use the
lxc config showcommand followed by the container name.
lxc config show mycontainer
- To remove the added directory from the container, use the
lxc config device removecommand 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.