How to configure a static IP address for a debian bullseye container

I used to launch new debian containers with a little bash script that basically :

  1. launch a container with lxc launch images:debian/bullseye "$NAME"
  2. configure the network by pushing simple /etc/network/interfaces and etc/resolv.conf files
cat <<EOF > /tmp/$NAME.interfaces
auto eth0
iface eth0 inet static
	address $IPV4
	gateway $GATEWAY_IPV4
	netmask 255.255.255.0

iface eth0 inet6 static
	address $IPV6
 	netmask 64
 	gateway $GATEWAY_IPV6
EOF
lxc file push "/tmp/$NAME.interfaces" "$NAME/etc/network/interfaces"

cat <<EOF > "/tmp/$NAME.resolv.conf"
search lxd
nameserver $DNS
EOF
lxc file push "/tmp/$NAME.resolv.conf" "$NAME/etc/resolv.conf"
  1. install some packages I always need and create my user.

This was still working fine a few months ago (I managed to launch a bullseye container this way in February). But more recently it started failing because of networking problem (ip adress is not well configured anymore, there is no internet connection inside the container and it fails to download packages).

It seems recent bullseye LXC containers doesnt have installed the networking/ifup/ifdown services, nor /etc/network folder (so pushing /etc/network/interfaces fails).
Thanks to this answer Problem inside Debian/Bullseye image networking service when creating a container via lxc-create -t download from @tomp , I understand it uses systemd network now, which i’m not very familiar with.

  • Should I try configuring and pushing /etc/systemd/network instead of /etc/network/interfaces in my script?
cat <<EOF > "/tmp/$VM_NAME.eth0.network"
[Match]
Name=eth0
[Network]
Address=$IPV4/24
Gateway=$GATEWAY
Address=$IPV6/64
Gateway=2a09:7340::254
EOF
lxc file push "/tmp/$NAME.eth0.network" "$NAME/etc/systemd/network/eth0.network"
lxc exec "$NAME" -- sh -c "systemctl restart systemd-networkd"

seems to work fine!

  • Is it a reasonable way to configure a static IP address on a container? Or would it be preferable to use a lxc config?
  • What about /etc/resolv.conf? I can still push it but it is reset after a reboot.

Thanks for your help/advice!

This approach is fine. Or if you use a managed lxd network then using a static dhcp allocation using lxc config device set <instance> eth0 ipv4.address=n.n.n.n works too.

That has the advantage of setting the resolver via dhcp too

Thanks for your input!