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?
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.
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)
'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With