15

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.

1
5

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.

2
  • 1
    I am a little confused by this solution: it says he needs to setup a bridge, then says the solution is to turn off the default bridge setup. Surely he must switch ON a bridge setup. The second thing I need help with is that User says that the bridge setup is NATed, Surely it's either bridged (so the containers is on the same network as the host) or is NATed (on a different network and requires routing). My understanding is that these are mutually exclusive? – John Little Mar 22 '16 at 21:31
  • 1
    This is all invalid now with the current versions. /etc/default/lx* is completely gone. – spyderdyne Feb 13 '17 at 21:15
5

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.

2
  • @JonathanY. The br0 should appear after adding it to /etc/network/interfaces as stated in the answer. You might need a reboot. – Epeli Jan 14 '15 at 8:58
  • 3
    Thanks. What I was missing was needing to add <username> veth br0 2 to /etc/lxc/lxc-usernet in order to use br0 with unprivileged containers. – Jonathan Y. Jan 14 '15 at 16:09
2

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

3
  • 1
    Nice approach - try. Thumbs up! Adding static route works - and router itself can ping containerized machines! But with many ISP supplied routers port forwarding won't work due to: Error code: 4937 The IP address is not in the same subnet with LAN IP address. Please input another one. (router holds 192.168.x.x subnet, whilst LXD/LXC are on 10.0.x.x subnet) – stamster Jun 12 '17 at 12:03
  • 1
    Yes, your router will need to accept a subnet mask for my solution to work. I like to buy a router and add an after-market firmware to make crazy configurations possible :-p – thirdender Jun 13 '17 at 0:31
  • 1
    I'm using MikroTik at both home and office, but this was for one setup where they had plain router supplied by ISP. So your idea is very simple yet very effective - KISS principle :) I like it and will use it for sure as I cannot get it how those containers they don't have yet a solution to expose them into external world. – stamster Jun 13 '17 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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