Created
June 18, 2015 15:55
-
-
Save subfuzion/6f40b933b685ef379175 to your computer and use it in GitHub Desktop.
Get dynamically assigned ports in Docker
This file contains hidden or 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 port { | |
if [[ $2 ]]; then | |
FORMAT='{{(index (index .NetworkSettings.Ports "'$2'/tcp") 0).HostPort}}' | |
elif [[ $1 ]]; then | |
FORMAT='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' | |
else | |
echo "error: missing container-id" | |
echo "usage: port <container-id> [port]" | |
return 1 | |
fi | |
docker inspect --format="${FORMAT}" $1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OR just use
Doh! 😲 Thanks for the tweet, Jessie!