LXD containers are brilliant, but lacks an easy way to forward ports from the containers to the host. One can use iptables manually, of course, but I really missed something easy like Docker. To try and remedy this, I have conjured up a little bash script. With this script, you can add, delete and list port forwarding rules.
It’s a bit rough around the edges, but maybe I’ll tidy it up a bit some day.
Read on for the script and examples.
Adding a rule
Let’s say you have an SMTP server running in a container named ‘mailbox’. To forward port 25 on the container to port 25 on the host, just do this:
Another example, what if you have a web server running in the container ‘tomcat’, on port 8080, and want it forwarded to port 80 on the host?
Listing rules
To list rules, simply type:
Sample output:
Deleting a rule
To delete a rule, get the rule number from the ‘list’ command, e.g. 2, and do:
The script
Here’s the whole thing. Keep in mind that some things are hard coded, like the ethernet device, so some modification may be required for it to run in another environment.