Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Created June 18, 2015 15:55
Show Gist options
  • Save subfuzion/6f40b933b685ef379175 to your computer and use it in GitHub Desktop.
Save subfuzion/6f40b933b685ef379175 to your computer and use it in GitHub Desktop.
Get dynamically assigned ports in Docker
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
}
@subfuzion
Copy link
Author

OR just use

docker port <container-id>

Doh! 😲 Thanks for the tweet, Jessie!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment