Skip to content

Instantly share code, notes, and snippets.

@vierbergenlars
Created January 23, 2019 10:22
Show Gist options
  • Save vierbergenlars/1124e5ce6cf9c9968204fcc91e80128b to your computer and use it in GitHub Desktop.
Save vierbergenlars/1124e5ce6cf9c9968204fcc91e80128b to your computer and use it in GitHub Desktop.
Bridge a port that is exposed inside a container to your host
#!/bin/bash
# Call as docker-intercept <container-name> <port-inside-container> <port-on-host>
set -e
container="$1"
container_port="$2"
host_port="$3"
network_id="$(docker inspect "$container" | jq '.[0].NetworkSettings.Networks | to_entries | .[0].value.NetworkID' -r)"
container_ip="$(docker inspect "$container" | jq '.[0].NetworkSettings.Networks | to_entries | .[0].value.IPAddress' -r)"
docker run -ti --rm --net "$network_id" -p "$host_port:$host_port" bobrik/socat "TCP4-LISTEN:$host_port,fork,bind=0.0.0.0" "TCP4:$container_ip:$container_port"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment