LXD container with static IPv4 address?
By default, a new LXD container is assigned a dynamic IPv4 address by the internal LXD DHCP server. However, in many cases it is necessary to assign a static IP address, for example to make a web server available on a separate container behind a proxy server running on another container on the same host, or simply to make the container accessible from the outside via the network bridge by using IP tables.
In this example we will assign IP address “10.0.30.10 to our container “web-server”. The default profile of LXD is configured with bridge “lxdbr0” and IP address 10.0.30.1 with the following DHCP range “10.0.30.2-10.0.30.254”.
LXD does not actually attach a device called eth0 directly to the container and as such the network configuration for the eth0 device that we inherited from the default LXD profile needs to be modified to allow us to set a static IP address. This can be done in 2 ways. The new way is to override the setting of eth0 device of our container and at the same time set the desired IP address.
# lxc config device override web-server eth0 ipv4.address=10.0.30.10
Alternatively we can attach the existing bridge “lxdbr0” to the NIC “eth0” of our container which is the old method. Obviously you will need to skip the previous command.
# lxc network attach lxdbr0 web-server eth0
When done the static IP address can be set.
# lxc config device set web-server eth0 ipv4.address 10.0.30.10
The above command can also be used to change an already set IP address.
Finally we need to restart the container.
# lxc restart web-server
If anything goes wrong or you just want to reset the default network settings then you can remove the eth0 device.
# lxc config device remove web-server eth0
A restart of the container will be required for the settings to take effect and to obtain an IP address from the DHCP server.
# lxc restart web-server