Sub topic 4
Logging in from the command line
This chapter assumes you have Docker installed and running, a free Docker Hub account, and a local image such as myapp:1.0. You should be comfortable in a terminal. Here you will authenticate the Docker CLI against Docker Hub so that later chapters can push images.
What `docker login` does
Docker Hub is a registry, a server that stores images. Pushing to your repository requires proof of who you are. The docker login command sends your credentials to the registry. If they are valid, the CLI stores a credential in your Docker configuration, and later docker push and docker pull commands reuse it.
Pulling public images needs no login. Pushing always does. Pulling from a private repository does too.
Logging in interactively
Run the command with your Docker Hub username:
docker login -u YOUR_USERNAMEDocker prompts for a password. Typed characters are not echoed. Because the secret is entered at the prompt, it never appears in your shell history.
A successful login prints Login Succeeded. If you get an authentication error, check the username first. Docker Hub usernames are your account ID, not your email address, and they are lowercase.
Use a token, not your password
Docker Hub lets you create a personal access token (PAT). A PAT is a generated secret that works in place of your password for the CLI and for scripts. It has two advantages:
- You can limit its permissions, for example to read and write on repositories only.
- You can revoke it without changing your account password.
To create one, open Docker Hub, go to your account settings, find the security section, and choose to create a new access token. Give it a description such as laptop-cli, pick the permission level, and copy the value when it is shown. Docker Hub displays the token only once.
At the password prompt of docker login, paste the token. The command is the same as before.
Logging in from a script
Scripts should not use the interactive prompt, and they should never put a secret on the command line as -p. Command-line arguments are visible in process listings and may be saved in logs. Use --password-stdin and read the token from an environment variable:
echo "$DOCKERHUB_TOKEN" | docker login -u YOUR_USERNAME --password-stdinThe pipe sends the token to the CLI through standard input, so it does not appear in the argument list. The variable DOCKERHUB_TOKEN must already be set in the environment, for example by your CI system's secret store.
Where the credential is stored
After login, Docker records the credential for the registry. The location depends on your setup:
| Setup | Where the credential goes |
|---|---|
| Docker Desktop | The operating system's credential store, through a helper |
| Linux with a credential helper configured | The helper's secure store |
| Linux with no helper | `~/.docker/config.json`, encoded but not encrypted |
The last row deserves care. The value in config.json is base64 text, which anyone with read access to the file can decode. Treat that file like a password. On shared machines, configure a credential helper, or run docker logout when you finish.
To check which registries you are logged in to, inspect the auths section of the file:
cat ~/.docker/config.jsonIf you see a credsStore entry instead of stored values, a helper is handling the secret.
Logging out
docker logoutThis removes the stored credential for Docker Hub. Run it on any machine you do not own or will not use again.
Common login problems
unauthorized: incorrect username or password: the username, password, or token is wrong, or the token was revoked. Create a new token and try again.- Login works, but push is denied: authentication succeeded, but your account lacks write access to the repository, or the image name uses the wrong namespace. Later chapters cover this.
- Prompt does not accept pasted text: some terminals hide pasted input. The paste still works, so press Enter.
- Two-factor authentication enabled: your account password will not work in the CLI. Use a personal access token.
Exercise
Why is echo "$DOCKERHUB_TOKEN" | docker login -u YOUR_USERNAME --password-stdin preferred over docker login -u YOUR_USERNAME -p "$DOCKERHUB_TOKEN" in a script?
Show answer
With -p, the token is part of the command's arguments, which other users can see in process listings and which may end up in logs or shell history. With --password-stdin, the token travels through standard input and is not exposed in the arguments.
Exercise
You run docker login on a Linux server with no credential helper. Where is the credential stored, and what should you do when you finish?
Show answer
It is stored in ~/.docker/config.json as base64 text, which is not encrypted. Protect the file, and run docker logout when you no longer need access, so the stored credential is removed.