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

Bruno Bronosky picture Bruno Bronosky · Mar 29, 2015 · Viewed 19.5k times · Source

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?

Answer

Kevin Cantwell picture Kevin Cantwell · Aug 13, 2016

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.