Reputation: 70459
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?
Upvotes: 24
Views: 69277
Reputation: 7
If you want to see which user is executing Docker client commands on the host, you can use:
command whoami
Upvotes: -2
Reputation: 70459
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
Upvotes: 24
Reputation: 1170
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.
Upvotes: 20
Reputation: 70459
This works to extract it, but technically it's a race condition.
username="$(sed 's/.*(//;s/).*//' <(docker login & sleep 1; kill %1))"
Surely there is something better.
Upvotes: 0