LXD container IP address changed

Hi LXD folks, I need your support to resolve following issue.
I have created bunch of lxd containers using juju budle, and I added two containers manually. Everything was working properly. After VM host holding lxd containers was restarted my juju controller lxd container got different IP address (172.30.30.174) comparing to initial one 172.30.30.175 received during the deployment. The later IP was assigned to completely different container without juju service. So I could not reach controller due to IP mismatch. I manually exchanged IPs between those two containers but after some time previous IP got assigned back ( juju-212b45-0 and pro-on-prem):

lxc list
±---------------------------±--------±---------------------±-----±----------±----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
±---------------------------±--------±---------------------±-----±----------±----------+
| airgapped-snap-store-proxy | RUNNING | 172.30.30.119 (eth0) | | CONTAINER | 0 |
±---------------------------±--------±---------------------±-----±----------±----------+
| juju-212b45-0 | RUNNING | 172.30.30.175 (eth0) | | CONTAINER | 0 |
| | | 172.30.30.174 (eth0) | | | |
±---------------------------±--------±---------------------±-----±----------±----------+
| juju-f21f6d-0 | RUNNING | 172.30.30.13 (eth0) | | CONTAINER | 0 |
±---------------------------±--------±---------------------±-----±----------±----------+
| juju-f21f6d-1 | RUNNING | 172.30.30.164 (eth0) | | CONTAINER | 0 |
±---------------------------±--------±---------------------±-----±----------±----------+
| juju-f21f6d-2 | RUNNING | 172.30.30.249 (eth0) | | CONTAINER | 0 |
±---------------------------±--------±---------------------±-----±----------±----------+
| juju-f21f6d-3 | RUNNING | 172.30.30.111 (eth0) | | CONTAINER | 0 |
±---------------------------±--------±---------------------±-----±----------±----------+
| pro-on-prem | RUNNING | 172.30.30.175 (eth0) | | CONTAINER | 0 |
| | | 172.30.30.174 (eth0) | | | |
±---------------------------±--------±---------------------±-----±----------±----------+

Is it expected behavior that IP address of the lxd container has changed after host restart? I though lxd dnsmask binds an IP addresses based on lxd icontainer interface mac address and they stay static. Is there any efficient way to assign proper IP to the container on lxd managed network without risk to be reassigned to another?

Hi, please see this link why the address might change during reboot: Container getting different IP Address on LXD host reboot. · Issue #6667 · canonical/lxd · GitHub

You can use a static IP address by setting ipv4.address on the containers network interface: https://documentation.ubuntu.com/lxd/en/latest/reference/devices_nic/#device-nic-bridged-device-conf:ipv4.address.

The exact command can be found here Container getting different IP Address on LXD host reboot. · Issue #6667 · canonical/lxd · GitHub

Thanks @jpelizaeus this is very useful information. Just wandering how containers started by juju always get the same IP address despite host reboots. Is it a recommended approach to manually set static IP addresses even on a containers managed by juju? Because I observed this situation when multiple containers were present on the host and part of them were managed by juju and the rest were manually created and IP address switch occurred between manually created container and the one managed by juju.

I am afraid I cannot give a thorough answer on what is required when using juju, but from the perspective of LXD it should not matter who is creating the container.

The fact that non static DHCP IPs could be changed during reboots, will affect all containers (no matter who created them) that don’t use the ipv4.address setting on their interface.

What does your /etc/netplan config look like?

You mean /etc/neplan in lxd container? It is just simple:

network:
version: 2
ethernets:
eth0:
dhcp4: true

Try this and see if it helps:

network:
  ethernets:
    eth0:
      dhcp4: true
      dhcp-identifier: mac
  version: 2