By default, lxd containers get random ip from lxd-bridge system. If you want to assign static IPs (i.e with some logical or arithmetic order), good news: It is quite simple.
All we need to do is create a DNS configuration file and tell lxd to use it.
There is a configuration file lxd-bridge under /etc/default/ directory. It is created when you first run lxd init command. It is something like this:
# WARNING: This file is generated by a debconf template!
# It is recommended to update it by using "dpkg-reconfigure -p medium lxd"
# Whether to setup a new bridge or use an existing one
USE_LXD_BRIDGE="false"# Bridge name
# This is still used even if USE_LXD_BRIDGE is set to false
# set to an empty value to fully disable
LXD_BRIDGE="" # Update the “default” LXD profile
UPDATE_PROFILE="true"# Path to an extra dnsmasq configuration file
LXD_CONFILE="" # DNS domain for the bridge
LXD_DOMAIN="lxd" # IPv4
## IPv4 address (e.g. 10.0.8.1)
LXD_IPV4_ADDR="10.0.8.100"
## IPv4 netmask (e.g. 255.255.255.0)
LXD_IPV4_NETMASK="255.255.255.0"
Look that LXD_CONFILE. It is empty by default. We are going to put our configuration file’s path here. Let’s say we have 3 containers:
- web_server
- web_apps
- sip_server
Now create a file named dns.conf under /etc/default/ directory:
$ sudo nano /etc/default/dns.conf
Put these lines in it:
dhcp-host=web_server,10.0.8.99
dhcp-host=web_apps,10.0.8.100
dhcp-host=sip_server,10.0.8.101
The syntax is dhcp-host=container_name,ip_address.
Now update /etc/default/lxd-bridge
file:
## Path to an extra dnsmasq configuration file
LXD_CONFILE="/etc/default/dns.conf"
That’s it. To make our changes work, we need to restart lxd-bridge
service:
$ sudo service lxd-bridge restart
We may need to reboot each container to take their new ip address:
$ lxc exec web_server /bin/bash
root@web_server:$ reboot
Now each container will have static ip based on our dns.conf file even if you reboot the system.