5

When you create some containers on a host, e.g.:

sudo lxc-create -n container1 -t ubuntu
sudo lxc-create -n container2 -t ubuntu

the LXC system creates IP address for the hosts on a new subnet, e.g.

lxc-ls --fancy
NAME            STATE    IPV4        IPV6  AUTOSTART
----------------------------------------------------
container1  RUNNING  10.0.3.143  -     NO
container2  RUNNING  10.0.3.12   -     NO

The question is, where is the DHCP service which is allocating these addresses, and how can we allocated a fixed IP based on the server name or MAC address?

We tried editing /etc/lxc/default.conf and adding:

    dhcp-host=container1,10.0.3.10
    dhcp-host=container2,10.0.3.20

Then stopping and starting the containers and doing sudo service lxc-net restart but this had no effect. I could edit /etc/network/interfaces on each container and hard code an IP instead of using DHCP, but I would rather control it centrally.

Is there a way to do this?

eos
  • 551
  • 4
  • 10
  • 27

1 Answers1

6

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!

eos
  • 551
  • 4
  • 10
  • 27