Skip to content

Instantly share code, notes, and snippets.

@stj
Created April 27, 2021 23:45
Show Gist options
  • Save stj/2658ed400f80027f214a23589b11238e to your computer and use it in GitHub Desktop.
Save stj/2658ed400f80027f214a23589b11238e to your computer and use it in GitHub Desktop.
Reroute dynamic docker port to fixed local port
function pgreroute() {
re=':::([0-9]{1,5})->5432';
[[ $(docker ps -f "expose=5432") =~ $re ]];
port=${BASH_REMATCH[1]};
sudo iptables -t nat -I PREROUTING -p tcp --dport 5432 -j REDIRECT --to-ports $port;
sudo iptables -t nat -I OUTPUT -p tcp -o lo --dport 5432 -j REDIRECT --to-ports $port;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment