How to setup port forwarding/redirect for the LXC container

Objective

Configure port forwarding or port redirect on the Ubuntu 20.04, hosting LXC containers, to the container running web server on port 80.

Forward

To accomplish this we are going to add a proxy device called http-forward to the container called web-server that listens for connections on port 80. Using command:

lxc config device add web-server http-forward proxy \
listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80

 

Redirect

To configure a port redirect, all we have to change is port number that the host will listen to and device name. In this example port 8080 will forward to container web-server that is listening on port 80.

lxc config device add web-server http-redirect proxy \
listen=tcp:0.0.0.0:8080 connect=tcp:127.0.0.1:80

These commands are issues on the host. Nothing additional needs to be done to the container. Make sure to insert your own container name and device name that fits your needs.