Linux container bridge port forwarding

containerslxcport-forwarding

How to setup linux container bridge port forwarding?
I start using LXD, and looks like their policy is to setup port-forwarding with OS base unlike docker.

I want to build a web server, so 80 port shall be mapped to 80.

I have the following with one lxd container running:

$ ifconfig


lxcbr0    Link encap:Ethernet  HWaddr fe:f5:31:0c:4e:5d  
          inet addr:10.0.3.1  Bcast:0.0.0.0  Mask:255.255.255.0
          inet6 addr: fe80::f44f:9bff:fe8a:c309/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23 errors:0 dropped:0 overruns:0 frame:0
          TX packets:64 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1768 (1.7 KB)  TX bytes:9789 (9.7 KB)

veth2V2GC2 Link encap:Ethernet  HWaddr fe:f5:31:0c:4e:5d  
          inet6 addr: fe80::fcf5:31ff:fe0c:4e5d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23 errors:0 dropped:0 overruns:0 frame:0
          TX packets:34 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2090 (2.0 KB)  TX bytes:5251 (5.2 KB)


// and others

I suppose I need to bind:

  • lxcbr0: this should be lxd network bridge hub- 10.0.3.1, and
  • veth2V2GC2 : the container network interface.

Am I correct and can anyone show me the way? Thanks.

Best Answer

Unfortunately I cannot comment because of low reputation, so please do not downvote if I'm not correct.

I've googled around a d found this networking guide for "LXC", but it mostly seems to apply also to "LXD": http://www.flockport.com/lxc-networking-guide/

Within this article there is part "Deploy containers in cloud KVMs". Portforwarding is explained here, so please try this (and adopt the command for the IPs in your environment:

iptables -t nat -I PREROUTING -i eth0 -p TCP -d 1.1.1.1/32 --dport 80 -j DNAT --to-destination 10.0.3.165:80