LXC container with bridged IP


LXC (Linux Containers) is an operating-system-level virtualization method for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel and a great alternative to Docker.

Within Ubuntu 16.04 there is on top container manager, called LXD. It’s basically an alternative to LXC’s tools and distribution template system with the added features that come from being controllable over the network.

The default network setup for a new LXC container within Ubuntu is a bridged one, based on the parent interface lxdbr0. Your container will receive an internal IP address from the LXD own managed DHCP server.

But if you want an IP address from your local LANs DHCP server you have to follow these guide.

  1. Convert your primary network interface to a bridged one. Edit your /etc/network/interfaces on the host with your prefered editor under root right and add the following lines
    auto br0
    iface br0 inet static (or dhcp)
    bridge_ports yourphysicalnetworkinterface
    further static IP details

    The following could be a sample interfaces file

    # This file describes the network interfaces available on your system
    # and how to activate them. For more information, see interfaces(5).
    
    source /etc/network/interfaces.d/*
    
    # The loopback network interface
    auto lo
    iface lo inet loopback
    
    # The primary network interface
    auto br0
    iface br0 inet static
    bridge_ports eth0
    address 192.168.178.200
    netmask 255.255.255.0
    network 192.168.178.0
    broadcast 192.168.178.255
    gateway 192.168.178.1
    dns-nameservers 192.168.178.1 192.168.178.2
  2. Create a LXC network profile
    lxc profile create bridged
  3. Edit the new LXC network profile
    lxc profile edit bridged
  4. Edit the new LXC network profile
    config: {}
    description: bridged
    devices:
      eth0:
        name: eth0
        nictype: bridged
        parent: br0
        type: nic
    name: bridged
    used_by: []
  5. Reboot your server
  6. Stop your existing LXC container
    lxc stop containername
  7. Apply your new created network settings to the container
    lxc profile apply containername bridged
  8. Start your container
    lxc start containername

And now, even everything is fine, your LXC container will receive an IP address from your local LANs DHCP server and could look like this

user@system:/home/user# lxc list
+------------+---------+----------------------+------+------------+-----------------+
|    NAME    | STATUS  |         IPV4         | IPV6 |    TYP     |    SNAPSHOTS    |
+------------+---------+----------------------+------+------------+-----------------+
| CONTAINER1 | RUNNING | 10.253.16.185 (eth0) |      | PERSISTENT | 0               |
+------------+---------+----------------------+------+------------+-----------------+
| CONTAINER2 | RUNNING | 192.168.178.65 (eth0)|      | PERSISTENT | 0               |
+------------+---------+----------------------+------+------------+-----------------+
| CONTAINER3 | RUNNING | 10.253.16.205 (eth0) |      | PERSISTENT | 0               |
+------------+---------+----------------------+------+------------+-----------------+
| CONTAINER4 | STOPPED |                      |      | PERSISTENT | 0               |
+------------+---------+----------------------+------+------------+-----------------+