You can restrict VPS incoming connections within your VPS provider’s admin page (at a network-level), but if you can’t restrict outgoing connections, or you prefer to use a firewall at a server-level. you can use ufw.

This is an example of how ufw works. We are going to restrict our incoming connections to a specific IP. Replace “IP” with your chosen IP.

You may need to start and enable ufw service.

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow from IP
sudo ufw enable

To allow a specific port:

sudo ufw allow 1050

If you use Docker, -p flag makes the selected port available to everyone regardless of your ufw configuration, so you need to use --network host instead.

docker run -d --network host nginx