LXD containers and networking with static IP
I found a solution here
Thanks to Stéphane Graber.
Quote:
"If running a modern LXD with an LXD managed bridge, then you can just set the ipv4.address property on the network interface of the container.
- lxc stop c1
- lxc network attach lxdbr0 c1 eth0 eth0
- lxc config device set c1 eth0 ipv4.address 10.99.10.42
- lxc start c1
"
If you want to specify ip addresses for containers in LXD, look at the /etc/default/lxd-bridge
file. There you will find a spot to include an external dnsmasq configuration file.
Assuming you are on Ubuntu 16.04,
Open up /etc/default/lxd-bridge
in your favorite editor. You will need to use sudo
.
At around line 16,
LXD_CONFILE=""
Add an entry that points to a dnsmasq configuration file. You are going to have to create this file. So name it whatever you wish. Something like lxd_bridge.conf
.
Then create and edit the file that you have named above.
Add a line for each container that you want to assign a specific ip address to.
Like this:
dhcp-host=containername,ipaddress
Then you will need to restart lxd-bridge and then restart the containers.
Here's a detailed article about this:
LXD Static IPs
Note that if you are running Alpine linux in your containers, additional steps need to be taken to make this work. alpine
System info: lxc-3.0.1
on Ubuntu Server 18.04.1
.
After a lot of searching, I found this simple command to assign a static ip to a container:
lxc config set [container] raw.lxc 'lxc.net.[i].ipv4.address = [ip]/[subnet-mask]'
where [container]
, [i]
, [ip]
and [subnet-mask]
are the container name, network interface number, desired static ip address, and the CIDR for the desired subnet mask, respectively.
For instance, if you want to assign 240.10.0.20
with subnet mask 255.255.255.0
to the 0'th network interface of the container named hello
, you can use:
lxc config set hello raw.lxc 'lxc.net.0.ipv4.address = 240.10.0.20/24'
You will probably need to restart the container after executing the command.
Note that this will probably not change the current IP address of the container (in my setup anyway); the container should be reachable at both its original IP and the newly assigned IP. As a result, the container entry in lxc list
might contain several IP addresses.
Take a look here for the acceptable configuration keys appendable to lxc.net.[i]
. This webpage contains information on the CIDR notation.
Unfortunately, I cannot remember where I first found this solution. Here is a relevant GitHub issue that might contain useful information.