LXD Commands

Yes, this is a post related to my most-used LXD commands.

LXD is a frontend of LXC.

I. Mount a host directory

Add a device typed “disk”.

lxc config device add <containerName> <deviceName> disk path=/path/in/container source=/path/on/host -v

Example: lxc config device add b01 cert disk path=/cert source=/opt/cert -v

Here, /opt/cert on the host can now be accessed in container b01 at /cert.

II. Transferring files

Sometimes we just want to transfer a single file.

lxc file push /path/to/your.file <containerName>/part/to/target.file

# or omit target file name, defaults to the same as source.
lxc file push /path/to/your.file <containerName>/part/to/

Example: lxc file push ./backend-0.0.1-SNAPSHOT.jar b01/root/backend.jar

We pushed ./backend-0.0.1-SNAPSHOT.jar to container b01, at /root/backend.jar.

… and similarly, change push to pull can obtain files in container.

III. NVIDIA computing

Accessing physical GPU in container is exceedingly useful when you want an isolated environment.

lxc launch <imageName> <containerName> -c nvidia.runtime=true -c nvidia.driver.capabilities=compute,utility

Example: lxc launch images:ubuntu/22.04 cuda-test -c nvidia.runtime=true -c nvidia.driver.capabilities=compute,utility

This makes it possible to add GPU to container later. Note that at this point, the container still cannot access any GPU.

Then add a GPU device to your container.

lxc config device add <containerName> gpu gpu

At this point, the GPU is available to be used in your container.

For Arch Linux users

The default installation of NVIDIA driver and LXC & LXD is not ready for in-container computing. An additional utility, named nvidia-container-toolkit, should be installed (from AUR) prior to any of commands mentioned above.

IV. Port forwarding

We may want to expose some ports inside containers to the Internet.

lxc config device add <containerName> <deviceName> proxy listen=tcp:<IP:port> connect=tcp:<IP:port>

Example: lxc config device add b01 tcp8081 proxy listen=tcp:0.0.0.0:8081 connect=tcp:127.0.0.1:8080

This command adds a device, named tcp8081, to container b01, listens at 0.0.0.0 on TCP port 8081, then forward to 127.0.0.1:8080 of container b01.

??. To be continued

This is a dynamic and incomplete list. More useful commands may be added in the future. Let’s make it more complete, if you have any idea!


Last modified on 2022-05-27