lxc-expose-port is a simple script that sets up port forwarding for LXC containers. It was influenced by https://github.com/evanhempel/lxc-portforward but is not a fork of that project.
Unlike lxc-portforward, lxc-expose-port correctly handles connections to the external port from the host itself and from other containers. Also, it does not need any extra configuration file.
The script, as it is, works on Ubuntu 14.04 and 16.04 only. To support other systems, replace this line
. /etc/default/lxc-net
with a line that sets $LXC_NETWORK to the network used by LXC containers, in CIDR notation (e.g., to 10.0.3.0/34).
The use of Ubuntu 14.04 is supported only with the updated kernel. The reason is that kernels before 3.19 refused to send a DNATed packet back to the bridge port it originated from, thus breaking container communication with itself.
Copy lxc-expose-port to /etc/lxc. Then, in your container config, add something like this:
# for LXC 1.x:
lxc.network.script.up = /etc/lxc/lxc-expose-port 8080:80 2222:22 udp/9999:9999
lxc.network.script.down = /etc/lxc/lxc-expose-port
# for LXC 2.x and 3.x:
lxc.net.0.script.up = /etc/lxc/lxc-expose-port 8080:80 2222:22 udp/9999:9999
lxc.net.0.script.down = /etc/lxc/lxc-expose-port
The example above will cause the host's TCP ports 8080 and 2222 to be forwarded to ports 80 and 22 of the container, and will also forward UDP port 9999 to port 9999 of the container.
LXC Web Panel (https://github.com/claudyus/LXC-Web-Panel) is a convenient tool for managing LXC containers with a web browser. Unfortunately, it does not allow spaces and colons in lxc.network.script.up. The problem is merely due to a too-strict validation rule, and can be fixed. Edit lwp/utils.py and change this line:
file_match = '^\/\w[\w.\/-]+$'
to this:
file_match = '^\/\w[\w.\/: -]+$'
and then restart lwp.
Note: LXC Web Panel is not really compatible with LXC later than 1.x.
The lxc-expose-port script bears the following copyright:
Copyright (C) 2016-2018
Author: Alexander E. Patrakov <[email protected]>
Sponsored by: NobleProg Ltd <https://www.nobleprog.co.uk>
Distributed under the MIT license, see the LICENSE file.