Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get the Docker Hub username of the current user?

I see that ~/.dockercfg has your login credentials, but it is your email and not your username. I see that running docker login displays your username and prompts you to change it. But, you can you just get your username to put into a build script?

like image 753
Bruno Bronosky Avatar asked Mar 29 '15 07:03

Bruno Bronosky


2 Answers

Display the username with:

docker info | sed '/Username:/!d;s/.* //'

Store it in a variable with:

username=$(docker info | sed '/Username:/!d;s/.* //'); 
echo $username

Note that if you have bash history expansion (set +H) you will be unable to put double quotes around the command substitution (e.g. "$(cmd...)" because ! gets replaced with your last command. Escaping is very tricky with these nested expansions and using it unquoted as shown above is more readable and works.

like image 159
Kevin Cantwell Avatar answered Oct 20 '22 00:10

Kevin Cantwell


Update for modern Docker.

Seems that docker info no longer contains username. I have instead learned to extract it from the credential store via jq. I haven't tried this on every credStore, but for the ones I have checked on macOS and Linux, this works.

# line breaks added for readability; this also works as a oneliner
docker-credential-$(
  jq -r .credsStore ~/.docker/config.json
) list | jq -r '
  . |
    to_entries[] |
    select(
      .key | 
      contains("docker.io")
    ) |
    last(.value)
'

[bonus] Show the entire contents of your credential helper

In this case I've settled on docker-credential-desktop, but it should work with any. You can extract your credential helper from your Docker config as I did in the previous code block.

docker-credential-desktop list | \
    jq -r 'to_entries[].key'   | \
    while read; do
        docker-credential-desktop get <<<"$REPLY";
    done
like image 27
Bruno Bronosky Avatar answered Oct 19 '22 23:10

Bruno Bronosky