Map LXC Container Port to Host Port

If you are using LXD or Incus, forwards would be made with those tools. If you are using just LXC Containers, you need to configure the port forward with iptables on the host.

Forward port 80 on the host to the LXC container. In this example the LXC container has an IP of 10.0.3.133. If you haven’t already, you should make sure you have set a dhcp reservation for your container, so you have a predictable IP. I go over this in my post on how to Use LXC Containers on Ubuntu.

$ iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 10.0.3.133:80
$ iptables-save

Quick Breakdown of Iptables Options

  • -t nat specifices the “nat” table
  • -A PREROUTING appends to the PREROUTING chain
  • -p specifies the protocol
  • –dport specifies the destination port
  • -j DNAT means we should jump the packet to the DNAT target.
  • –to-destiation {ipaddress}:{port} is a required option of the DNAT target that specifies where the packet should be sent

Leave a Reply