Sub topic 10
Using access tokens instead of your password
This chapter assumes you can build an image, have a free Docker Hub account, and have already run docker login once. You will replace your account password with a personal access token (PAT). A PAT is a revocable credential that Docker Hub generates for you, with its own name and permission level.
Why not use your password?
Your password unlocks the whole account, including settings, billing, and two-factor setup. If it leaks from a script or a CI log, an attacker gets all of that. A token limits the damage in three ways:
- It can be limited to read, write, or delete access on repositories.
- You can create one token per machine or pipeline and revoke each one on its own.
- If you use two-factor authentication, the CLI needs a token anyway, because it cannot answer a second-factor prompt.
Create a token
- Sign in to Docker Hub and open Account settings, then Personal access tokens.
- Choose Generate new token.
- Enter a description that names where the token will be used, such as
laptop-cliorgithub-actions-myapp. - Pick an access permission. Use Read & Write for pushing. Use Read-only for machines that only pull.
- Copy the token immediately. Docker Hub shows it once. If you lose it, delete it and generate another.
Store the copy in a password manager or in your CI system's secret store. Do not put it in a Dockerfile, a source file, or a repository.
Log in with a token
For interactive use, run the same command as before. When it asks for a password, paste the token.
docker login -u YOUR_USERNAMEThe username stays the same. Only the secret changes. Docker Hub treats the token as the password.
For scripts, read the token from an environment variable and pass it on standard input:
echo "$DOCKERHUB_TOKEN" | docker login -u YOUR_USERNAME --password-stdin--password-stdin keeps the token out of the process list and your shell history. Avoid the -p flag. It puts the secret in the command line, where other users on the machine can see it with ps.
Where Docker stores the credential
After a successful login, Docker saves the credential so later pushes do not ask again. On Linux without a credential helper, it writes it to ~/.docker/config.json in base64 form. Base64 is an encoding, not encryption, so anyone who can read that file can use the token.
Prefer a credential helper where you can. On macOS and Windows, Docker Desktop uses the system keychain by default. On Linux, you can install docker-credential-pass or a similar helper. Run docker logout when you finish on a shared machine.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| `unauthorized: incorrect username or password` | Token was pasted with a trailing space, or it was deleted | Generate a new token and paste it again |
| `denied: requested access to the resource is denied` on push | Token is read-only, or the image name uses another namespace | Create a Read & Write token and check the name |
| Login works in the terminal but fails in CI | Secret name is wrong or the secret is empty | Check the secret names in your CI settings |
Rotate and revoke
Treat tokens as short-lived. Rotate them on a schedule, and revoke one at once if it appears in a log, a commit, or a screenshot. In Personal access tokens, the list shows each token's description and when it was last used. Delete any token you do not recognize or no longer need. Deleting a token breaks every machine that uses it, which is why one token per use makes tracing easy.
Exercise
Why is docker login -u YOUR_USERNAME -p "$TOKEN" a worse choice than piping the token with --password-stdin?
Show answer
The -p flag places the token on the command line, so it can appear in shell history and in the process list that other users can read. --password-stdin reads the secret from standard input, so it never appears in either place.
Exercise
Your push fails with denied: requested access to the resource is denied, and the image name is correct. What token setting should you check?
Show answer
Check the token's access permission. A Read-only token can log in and pull but cannot push. Generate a Read & Write token and log in again.