dockerdockerhub

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?


Solution

  • 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