Skip to content

Instantly share code, notes, and snippets.

@mnylen
Last active September 13, 2018 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnylen/80d9d72139f91a970ceafb73a68546f8 to your computer and use it in GitHub Desktop.
Save mnylen/80d9d72139f91a970ceafb73a68546f8 to your computer and use it in GitHub Desktop.
Get JWT token to access private repositories on hub.docker.com
function echoerr {
echo "$@" 1>&2;
}
docker_username=${DOCKER_USER:-}
docker_password=${DOCKER_PASSWORD:-}
if [ -z "$docker_username" ]; then
if command -v docker-credential-osxkeychain > /dev/null 2>&1; then
readonly registry_url="https://index.docker.io/v1/"
set +e
credentials=$(echo $registry_url |docker-credential-osxkeychain get 2>/dev/null)
ret_val=$?
set -e
if [ $ret_val -eq 0 ]; then
docker_username=$(echo $credentials |jq -r ".Username")
docker_password=$(echo $credentials |jq -r ".Secret")
fi
fi
fi
# This time if it's not set, we have to exit
if [ -z "$docker_username" ]; then
echoerr "ERROR: Could not find valid docker credentials. Possible solutions:\n"
echoerr "- If you're on Mac OS X, go to Docker -> Preferences and select 'Securely store Docker logins in macOS keychain'. Might need to run docker login again."
echoerr "- Export \$DOCKER_USER and \$DOCKER_PASSWORD in your .bash_profile, .zshrc"
exit 1
fi
readonly docker_token=$(curl -s -H "Content-Type: application/json" -XPOST -d '{"username": "'${docker_username}'", "password": "'${docker_password}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
@mnylen
Copy link
Author

mnylen commented Sep 13, 2018

This function lets you get JWT token to access private repositories on hub.docker.com. It expects that either:

  • DOCKER_USER and DOCKER_PASSWORD are set; or
  • On Mac, that docker uses osxkeychain credentials store

Dependencies:

  • jq
  • curl

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