LXD — Assigning static IP to containers
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:
1 | # WARNING: This file is generated by a debconf template! |
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:
1 | $ sudo nano /etc/default/dns.conf |
Put these lines in it:
1 | dhcp-host=web_server,10.0.8.99 |
The syntax is dhcp-host=container_name,ip_address.
Now update /etc/default/lxd-bridge
file:
1 | ## Path to an extra dnsmasq configuration file |
That’s it. To make our changes work, we need to restart lxd-bridge
service:
1 | $ sudo service lxd-bridge restart |
We may need to reboot each container to take their new ip address:
1 | $ lxc exec web_server /bin/bash |
Now each container will have static ip based on our dns.conf file even if you reboot the system.