Sub topic 4
Logging in from the command line
This chapter covers how the Docker CLI authenticates against Docker Hub. It assumes you have Docker installed and running, a free Docker Hub account, and basic comfort with a terminal. You should also know how to build an image from a Dockerfile. By the end you will be able to log in interactively, confirm that the login worked, find out where your credentials are stored, and log out.
Why a push needs a login
Docker Hub lets anyone pull a public image without an account. Pushing is different. A push writes data into a repository under a namespace, and Docker Hub only accepts that write from a user who has permission for that namespace. The CLI proves who you are by sending credentials with each request to the registry.
Two separate things are involved:
- The Docker CLI (
docker) is the client you type commands into. - The Docker daemon is the background service that does the work, including talking to the registry.
docker login collects your credentials in the CLI and verifies them against the registry. After a successful login, later commands such as docker push and docker pull reuse those credentials without asking again.
The basic interactive login
The simplest form names your Docker Hub username and lets Docker prompt for the secret:
docker login -u YOUR_USERNAMEDocker then asks for a password. Typed characters are not echoed to the screen. On success you see this message:
Login SucceededPassing only -u and leaving out the password has a purpose. If you wrote the password on the command line with -p, it would be stored in your shell history and visible in the process list to other users on the machine. Docker prints a warning when you use -p for exactly this reason. Let the prompt collect the secret instead.
If you run docker login with no arguments, Docker prompts for both the username and the password. When you have no previous login, some Docker Desktop versions instead open a browser window and ask you to sign in on the website. Both paths end with the same result: the CLI holds a credential for Docker Hub.
Use an access token, not your account password
The prompt accepts either your account password or a personal access token. A personal access token is a long random string you create in your Docker Hub account settings, with its own name and permission level. Prefer a token for three reasons:
- You can revoke a token without changing your account password.
- You can limit a token to read-only, read and write, or read, write and delete access.
- If an account has two-factor authentication turned on, the command line cannot use the password at all. A token is the way in.
Tokens get their own chapter later in this tutorial, so for now you only need to know that you paste the token where the prompt asks for a password.
What happens during login
The following diagram shows the exchange between the CLI, the daemon, and the registry.
Notice that the CLI stores the credential after the registry accepts it. A wrong password never reaches your configuration file.
Where credentials are stored
Docker keeps login information in a JSON file named config.json. By default it lives in ~/.docker/config.json on Linux and macOS, and in %USERPROFILE%\.docker\config.json on Windows. You can point Docker at a different directory with the DOCKER_CONFIG environment variable.
What goes into that file depends on whether a credential store is configured. There are three cases.
| Setup | What `config.json` holds | Security |
|---|---|---|
| A credential helper is configured, such as `osxkeychain`, `wincred`, or `secretservice` | The name of the helper only. The secret lives in the operating system's keychain. | Best. The secret is protected by the OS. |
| A helper is not configured | A base64 string of `username:secret` under `auths` | Weak. Base64 is an encoding, not encryption. Anyone who can read the file can recover the secret. |
| `pass` credential helper | The name of the helper. The secret lives in a GPG-encrypted store. | Good, but you must set up `pass` first. |
Docker Desktop installs and configures a credential helper for you, so on a desktop machine the file usually contains lines like these:
{
"auths": {
"https://index.docker.io/v1/": {}
},
"credsStore": "desktop"
}The auths entry records which registry you are logged in to. The credsStore value names the helper that holds the actual secret. On a plain Linux server with Docker Engine only, there is often no helper, and Docker warns you at login time that the credentials are stored unencrypted:
WARNING! Your credentials are stored unencrypted in '/home/you/.docker/config.json'.
Configure a credential helper to remove this warning.Treat that warning seriously. Set chmod 600 on the file at a minimum, and avoid logging in with your main account on shared machines.
Confirming that you are logged in
Docker has no whoami command, so the CLI offers a few indirect checks.
- Run
docker loginagain. If a valid credential is stored, Docker reportsLogin Succeededwithout asking for anything. - Inspect
config.jsonand look forhttps://index.docker.io/v1/underauths. - On Docker Desktop, the Docker menu shows your signed-in username.
- Run
docker info. On many versions it prints aUsername:line when you are logged in to Docker Hub.
A listing that shows an entry only proves that a credential is stored. It does not prove the credential is still valid, because a revoked token still sits in the file until the next request fails. The definitive test is an authenticated action, which is the push you will do in a later chapter.
Logging in to a different registry
docker login with no server argument targets Docker Hub. To authenticate with a different registry, give the server name:
docker login ghcr.io -u YOUR_USERNAMEDocker stores one credential per registry, so you can be logged in to Docker Hub and GitHub Container Registry at once. The credential Docker sends is chosen by the registry hostname in the image name. An image named YOUR_USERNAME/myapp:1.0 has no hostname, so Docker assumes Docker Hub. An image named ghcr.io/YOUR_USERNAME/myapp:1.0 goes to GitHub. This is why image naming and login are linked: the name decides which login is used.
Logging out
To remove the stored credential for Docker Hub, run:
docker logoutFor another registry, name it, for example docker logout ghcr.io. Logging out deletes the entry from config.json or from the credential helper. It does not revoke the token on the Docker Hub side. If you want a token to stop working everywhere, delete it in your account's security settings.
Common login problems
Most login failures come from a short list of causes.
| Symptom | Likely cause | What to do |
|---|---|---|
| `unauthorized: incorrect username or password` | Typo, or you used your email address with the wrong secret | Use your Docker ID (username), not your email. Retype the secret, or create a new token. |
| `Error saving credentials: error storing credentials` | The credential helper named in `config.json` is missing or broken | Install the helper, or remove the `credsStore` line to fall back to file storage. |
| `Cannot perform an interactive login from a non TTY device` | You ran `docker login` in a script with no terminal for the prompt | Use `--password-stdin`, covered in the CI chapter. |
| Login works, but later commands say `denied` | The token has read-only scope, or the namespace in the image name is not yours | Check token permissions and the image name. |
| `Too Many Requests` or rate-limit messages | Many anonymous pulls from one network | Log in. Authenticated users get higher pull limits. |
A note on usernames
Docker Hub accepts your Docker ID for -u. Your email address may work for password logins on some versions, but it will not work with tokens, and it never forms part of an image name. Keep the habit of using the Docker ID everywhere.
Putting it together
The short workflow for this chapter is:
- Create a personal access token with read and write permission in Docker Hub.
- Run
docker login -u YOUR_USERNAME. - Paste the token at the password prompt.
- Confirm you see
Login Succeeded. - Check that
config.jsonholds an entry for Docker Hub, and that a credential helper is in use if one is available.
With a stored credential in place, the next steps are tagging your image for your namespace and pushing it.
Exercise
You ran docker login -u YOUR_USERNAME on a Linux server and saw Login Succeeded, plus a warning that credentials are stored unencrypted. Where is the secret, how readable is it, and what two actions reduce the risk?
Show answer
The secret is in ~/.docker/config.json under auths, as a base64 string of username:secret. Base64 is only an encoding, so anyone who can read the file can decode it. Reduce the risk by configuring a credential helper such as pass or secretservice so the secret is kept outside the file, and by restricting the file with chmod 600. Using a limited-scope access token instead of your account password also limits the damage if the file leaks, because you can revoke the token.
Exercise
You run docker logout on your laptop. Can someone who previously copied your access token still use it? Why or why not?
Show answer
Yes. docker logout only removes the credential stored on your machine. The token remains valid on Docker Hub until you delete or deactivate it in your account's security settings.