LXD containers and networking with static IP
I have done some Docker testing over the years, but for a specific kind of testing, I need a little more then just application containers. So I turned to LXC/LXD containers, which is great. After installing LXD I created my own container:
>> willem@ubuntu:/$ lxc launch images:centos/7/amd64 thing Creating thing
Starting thing
>> willem@ubuntu:/$ lxc list
+-------+---------+------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+-------+---------+------------------+------+------------+-----------+
| thing | RUNNING | 10.0.3.30 (eth0) | | PERSISTENT | 0 |
+-------+---------+------------------+------+------------+-----------+
Great. Only I don't seem to have any control over the IP address here. I found (with help of Google and locate) a control file:
>> root@ubuntu:/# locate lxc.conf
/etc/init/lxc.conf
..
/var/log/lxd/thing/lxc.conf
in which you can put a (what looks like) static IP4-address. Only upon rebooting the container, the host seem to have forgotten my static IP, and takes the one from DHCP. Questions:
- What is the right way of doing this (I mean, the latest Docker has an
--ip
switch indocker run
which sets the IP address) ?
These configurations are to be placed in the file /var/lib/lxc/ContainerName/config
. The possible values of the parameters are specified in the Manual. They pertain to different areas, hostname, network, console, ttys, mount point, cgroups, capabilities,...
Under network, you will find all you need. Sensible values are automatically produced for unspecified variables. The part most relevant to your question is the following:
lxc.network.name
the interface name is dynamically allocated, but if another name is needed because the configuration files being used by the container use a generic name, eg. eth0, this option will rename the interface in the container.
lxc.network.hwaddr
the interface mac address is dynamically allocated by default to the virtual interface, but in some cases, this is needed to resolve a mac address conflict or to always have the same link-local ipv6 address
lxc.network.ipv4
specify the ipv4 address to assign to the virtualized interface. Several lines specify several ipv4 addresses. The address is in format x.y.z.t/m, eg. 192.168.1.123/24. The broadcast address should be specified on the same line, right after the ipv4 address.
lxc.network.ipv4.gateway
specify the ipv4 address to use as the gateway inside the container. The address is in format x.y.z.t, eg. 192.168.1.123. Can also have the special value auto, which means to take the primary address from the bridge interface (as specified by the lxc.network.link option) and use that as the gateway. auto is only available when using the veth and macvlan network types.
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
"