How to set DHCP IPs of standard LXC containers?

Solution 1:

OK, found another way to do it which works:

  1. Stop all containers
  2. Edit /etc/default/lxc-net
  3. uncomment "LXC_DHCP_CONFILE=/etc/lxc/dnsmasq.conf"
  4. create the file /etc/lxc/dnsmasq.conf
  5. edit dnsmasq.conf:

    dhcp-host=container_name,10.0.2.10

  6. service lxc-net restart

Now restart the containers and bask in the glory of your own IPs.

-- EDIT --

Spoke too soon - this only half works.

There are 4 containers, and in the dnsmasq.conf I have:

dhcp-host=host01,10.0.2.10
dhcp-host=host02,10.0.2.20
dhcp-host=host03,10.0.2.30
dhcp-host=host04,10.0.2.40

Now when I restart evertying, including the containers, I get this:

# lxc-ls --fancy
NAME       STATE    IPV4        IPV6  AUTOSTART
-----------------------------------------------
host01    RUNNING  10.0.2.99   -     YES
host02    RUNNING  10.0.2.20   -     YES
host03    RUNNING  10.0.2.198  -     YES
host04    RUNNING  10.0.2.40   -     YES

It got half of them right. All containers we created in the same way, and have not been edited in any way.

Anyone has any ideas whats going on?

This is the contents of the /etc/network/interfaces on one of the offending containers:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

Nothing different to the working ones.

-- EDIT 2 ---

stopping all the containers, then running this:

$ sudo restart lxc-net

Fixed the issue!