How to limit resources for the Linux containers inside LXD.

Intro

In this example I’m using Ubuntu 20.04 with LXD 4.0.8 installed. Containers are running on ZFS storage. My test container name that I’m using for this tutorial is called lamp. What is lamp?

CPU and Memory limits

Use following commands to set CPU to 4 cores and Memory limit to 2GB for your container. Replaced container name with your own:

lxc config set lamp limits.memory 2048MB
lxc config set lamp limits.cpu 4


Root directory space limit

To set storage limit to 16GB, use:

lxc config device set lamp root size 16GB

If you got  Error: Device from profile(s) cannot be modified for individual instance. Override device or modify profile instead message. Use the following command:

lxc config device override lamp root size=16GB

 

Bonus

To find out how to configure port forwarding or port redirect, look here.