sudo ufw allow 22/tcp # or your SSH port: ALLOW SSH BEFORE ENABLING
sudo ufw allow 80,443/tcp
sudo ufw limit 22/tcp # slows brute-force attempts
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw enable
sudo ufw status numbered
sudo ufw delete 3 # remove rule number 3
sudo ufw allow from 192.168.1.0/24 to any port 5432 proto tcp
ufw writes iptables/nftables rules for you. Do not mix it with hand-written iptables rules unless you know what you are doing. Docker publishes ports by adding its own rules that bypass ufw, so a container port may be reachable even if ufw says denied; bind it to 127.0.0.1 in the compose file if it should stay private. On cloud providers the provider's firewall is a second layer.