Created
February 17, 2025 16:31
-
-
Save joshuacurtiss/26432a97a60741f9df6a7e2cb22d95fc to your computer and use it in GitHub Desktop.
Set up a temporary tunnel for a port to a container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tunnel_container () { | |
local container=$1 | |
local port=${2:-3306} | |
local network= | |
if [[ -z $container || $container == -h || $container == --help ]]; then | |
echo 'USE: tunnel_container <container> [port]' >&2 | |
return | |
elif ! network=$(docker container inspect -f '{{range $k, $v := .NetworkSettings.Networks}}{{$k}}{{break}}{{end}}' "$container") > /dev/null 2>&1; then | |
echo "Container $container does not exist." >&2 | |
return | |
elif [[ -z $network ]]; then | |
echo "Could not find network for $container." >&2 | |
return | |
fi | |
echo "Connecting you to $container port $port on the $network network. Press ctrl-c when done..." | |
docker run --rm --network="$network" -p "$port:$port" alpine/socat "TCP-LISTEN:$port,fork" "TCP:$container:$port" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment