Last active
September 13, 2018 06:07
-
-
Save mnylen/80d9d72139f91a970ceafb73a68546f8 to your computer and use it in GitHub Desktop.
Get JWT token to access private repositories on hub.docker.com
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 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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function lets you get JWT token to access private repositories on hub.docker.com. It expects that either:
DOCKER_USER
andDOCKER_PASSWORD
are set; orDependencies:
jq
curl