How do I setup an LXC guest so that it gets a dhcp address so I can access it on my network?

I setup LXC using:

lxc-create -t ubuntu -n lxcguest1

But I can't access any services running on it from my LAN, it appears to be NATed. How do I set it up so that I can access it (it probably needs to get it's dhcp address from my Linksys router)?

Thanks.


Solution 1:

I've just dealt with this issue myself. Basically you need to setup a bridge and bind your network card and container to it. Here is the article I followed:

http://bj0z.wordpress.com/2011/08/19/howto-build-a-base-lxc-container-in-ubuntu-11-04/

Sounds as if like me, you require the 'bridge' solution rather than the NAT solution. I also turned off the default LXC bridge setup (which is NAT'ed). To do this just edit the file: /etc/default/lxc and change USE_LXC_BRIDGE="TRUE" to USE_LXC_BRIDGE="FALSE" and reboot.

Solution 2:

My setup on Ubuntu 14.04.1 LTS (Trusty Tahr) hosts

Add to /etc/network/interfaces on the host

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address 0.0.0.0

auto br0
iface br0 inet dhcp
    bridge_ports eth0

(reboot after changes)

And on the container config files (/var/lib/lxc/containername/config) I set lxc.network.link = br0

With this the container will get public ip addresses from the dhcp server just like the host.

Solution 3:

I used the default Ubuntu LXC settings, and configured my router to send all traffic on 10.0.3.xxx to the Ubuntu machine. On a DD-WRT enabled router, the settings look something like the screenshot below. Replace 192.168.1.137 with the IP of the machine running LXC. Other routers should have similar options to set up a static route (here are static route instructions for Linksys, for example).

DD-WRT settings for static routing to LXC

This is unrelated, but I also used the DNSMasq service to point a hostname to the LXC container's IP address. This way I can access the container at http://gitlab/ anywhere on the network. In my opinion it's a lot easier to use a hostname to access a container than remember the IP address.

DD-WRT DNSMasq settings