6

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 in docker run which sets the IP address) ?
  • You might want to remove your last 2 bullet points. Requests for tutorials and books are off-topic. – DavidPostill Mar 2 '16 at 22:12
3

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.

  • Is this answer pertinent to LXD-managed containers as well? I don't have /var/lib/lxc, and all I have in /var/lib/lxd/<ContainerName> is metadata.yaml, templates and rootfs. (I'm on Ubuntu Xenial, LXD version 2.0.0.) – Jonathan Y. May 15 '16 at 21:43
  • I haven't tried it, but probably you can give these flags via the --config option to lxc launch. I haven't figured out how to create custom profiles yet. – Per Johansson Jun 1 '16 at 14:13
5

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

  • Welcome to Super User! Can you paraphrase the instructions for that link? This is just to prevent any adverse effects for link rot, in accordance with our policy. Thank you! – oldmud0 Aug 3 '16 at 19:25
  • No problem. Thanks for not booting me. – Jason Trickett Aug 3 '16 at 20:48
3

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'

Take a look here for the acceptable configuration keys appendable to lxc.net.[i]. This webpage contains information on the CIDR notation.

You will probably need to restart the container after executing the command.

Unfortunately, I cannot remember where I first found this solution. Here is a relevant GitHub issue that might contain useful information.

0

For anybody who is still struggling how to assign static ip adresses to the lxd containers.

My first attempt was to do it over dnsmasq so i added the ip adresses of the containers in /var/lib/lxd/networks/lxdbr0/dnsmasq.hosts/<container> and reloaded dnsmasq service. It did really work but did not persist after restarting the Host.

I thought it should be possible to achieve that with lxd tool. In fact it was mentioned in the lcd-doc. I didn't really know how to configure the key user.network-config using the command lxc config set <container> user.network-config <value>. Since i couldn't inject multiple key:values in the value argument.

  1. check the container subnet

    lxc network show lxdbr0
    
  2. fist create a yml (network.yml) file with the following value

    version: 1
    config:
      - type: physical
        name: eth1
        subnets:
          - type: static
            ipv4: true
            address: <ip>
            netmask: 255.255.255.0
            gateway: <gateway> // ends often with .1
            control: auto
      - type: nameserver
        address: 8.8.8.8       // for example
    
  3. create a container with this network configurations

    lxc launch ubuntu:18.04 test --config=user.network-config="$(cat network.yml)"    
    

voalá :)

I tried to change the ip address after creating the container

   lxc config set <container> user.network-config "$(cat network.yml)"

i restarted the container but the ip didn't update. If any body knows how to get it work after creating the container i will very grateful.

lxd version 3.0.1

0

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

"

New contributor
Dmitry Somov is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.