Sub topic 10
Using access tokens instead of your password
This chapter assumes you have Docker installed, a free Docker Hub account, and a local image you can push. It also assumes you know how docker login works at a basic level. You should be comfortable setting environment variables in your shell.
Why a token is better than a password
Your Docker Hub password unlocks your whole account. It can change your email address, delete repositories, and edit billing details. If you type it into a script, a CI system, or a shared machine, any leak exposes all of that.
A personal access token (PAT) is a separate credential that you create for a specific purpose. It has these properties:
- It works with
docker loginin place of a password. - You pick its permissions when you create it.
- You can give it a description such as
laptoporgithub-actions, so you know where it is used. - You can revoke it without changing your password or breaking other tokens.
- If your account has two-factor authentication (2FA) enabled, the Docker CLI cannot use your password at all. A token is the supported way to log in from the command line.
The last point is the one that most often forces the switch. With 2FA on, a password login fails even though the password is correct.
Permission levels
When you create a token you choose an access level. The levels Docker Hub offers are:
| Level | What the token can do | Typical use |
|---|---|---|
| Read-only | Pull images, including from private repositories you can see | Servers that only deploy images |
| Read & Write | Pull and push images | Developer machines and CI pipelines that publish |
| Read, Write, Delete | Pull, push, and delete images or tags | Cleanup jobs |
Choose the lowest level that does the job. A deployment server that only runs docker pull should never hold a token that can push. If that server is compromised, the attacker cannot overwrite your published images.
Docker may add or rename levels over time, so check the options shown on the creation page.
Creating a token
You create tokens in the Docker Hub web interface:
- Sign in to Docker Hub.
- Open your account settings and go to the security section, then personal access tokens.
- Choose to generate a new token.
- Enter a description that names where the token will be used.
- Set an expiration date if the page offers one, and select an access level.
- Generate the token and copy it.
Store the token in a password manager or a secret store. Do not put it in a Dockerfile, a source file, or a chat message.
Logging in with a token
The login command is the same one you used before. You supply your username, and you paste the token where it asks for a password:
docker login -u YOUR_USERNAMEThe prompt reads Password:. Paste the token there. The terminal does not echo it, so nothing appears as you paste. Press Enter.
Your username stays your Docker Hub username, not your email address. The token replaces only the password.
A successful login prints Login Succeeded. After that, docker push and docker pull work as they did with a password, within the limits of the token's access level.
Logging in from scripts
In a script you cannot type at a prompt. You might be tempted to use the -p flag:
docker login -u YOUR_USERNAME -p "$DOCKERHUB_TOKEN"Avoid this. Command-line arguments are visible to other users on the machine through process listings, and they are saved in your shell history. Docker prints a warning when you use -p for this reason.
Use --password-stdin instead. It reads the secret from standard input, so it never appears in the argument list:
echo "$DOCKERHUB_TOKEN" | docker login -u YOUR_USERNAME --password-stdinHere, DOCKERHUB_TOKEN is an environment variable that holds the token. Your CI system or secret manager sets it. You should not type the token into your terminal to set it, because that puts the value into shell history again. In an interactive session you can read it without echo:
read -rs DOCKERHUB_TOKEN
export DOCKERHUB_TOKENread -s hides what you type, and -r stops backslashes from being interpreted. The variable lives only in that shell session.
Where Docker stores your credentials
After a successful login, Docker saves the credential so you do not log in again for every command. Where it goes depends on your setup:
- If a credential helper is configured, Docker hands the credential to the operating system's secure store. Docker Desktop configures this for you, using the macOS Keychain, the Windows Credential Manager, or the Linux
passor secret service. - If no helper is configured, Docker writes the credential to
~/.docker/config.json, encoded in base64.
Base64 is an encoding, not encryption. Anyone who can read that file can decode the token in one command. Docker prints a warning that says your credentials are stored unencrypted when it falls back to this behavior.
You can check which helper is in use:
cat ~/.docker/config.jsonLook for a credsStore entry, for example "credsStore": "desktop" or "osxkeychain". If the file has an auths section with an auth value for https://index.docker.io/v1/ and no helper, the token is stored in that file.
On a shared server, set up a credential helper, or log out when you finish:
docker logoutdocker logout removes the stored credential for Docker Hub from the helper or the config file. It does not revoke the token itself. Revocation happens on Docker Hub.
One token per use
Create a separate token for each place you use one. A practical set looks like this:
| Token description | Level | Where it lives |
|---|---|---|
| `laptop-dev` | Read & Write | Your OS credential store |
| `github-actions-myapp` | Read & Write | A GitHub repository secret |
| `prod-server-pull` | Read-only | The production host |
Separate tokens give you three advantages. A leak in one place is contained to that token. You can revoke it without touching the others. The "last used" information on the token list tells you which tokens are still active, so you can delete the ones nobody uses.
Rotating and revoking tokens
Rotate tokens on a schedule, and revoke them immediately if you suspect exposure. The steps are:
- Create a new token with the same level and a new description.
- Update the place that uses it, such as the CI secret or the credential store.
- Confirm that a login or push works with the new token.
- Delete the old token in Docker Hub.
Doing the steps in this order avoids downtime, because the old token keeps working until step 4.
If a token appears in a public Git commit, treat it as compromised even if you delete the commit. Git history and forks keep copies, and automated scanners pick up leaked credentials within minutes. Revoke the token first, then clean up the repository.
How the pieces fit together
Docker Hub checks the token's access level at the time of each request. A read-only token logs in successfully, then fails when you try to push.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `unauthorized: incorrect username or password` | Wrong username, an email address used as the username, a mistyped token, or a revoked or expired token | Check the username, then create a new token and log in again |
| Login works with a password in the browser but fails in the CLI | 2FA is enabled | Log in with a token |
| `Login Succeeded` but `docker push` returns `denied: requested access to the resource is denied` | The token is read-only, or the image name does not match your namespace | Use a Read & Write token and check the image name |
| `Error saving credentials` | No working credential helper on the machine | Install or configure a helper, or remove a broken `credsStore` entry |
| Script works locally but fails in CI | The secret is empty, misnamed, or has trailing whitespace | Print whether the variable is set without printing its value |
For the empty-variable case, check safely:
if [ -z "$DOCKERHUB_TOKEN" ]; then echo "token is not set"; else echo "token is set"; fiThis tells you whether the variable has a value without revealing it in logs.
Checking that a token works
After you create a token, confirm that it behaves the way you intended before relying on it:
docker logout
echo "$DOCKERHUB_TOKEN" | docker login -u YOUR_USERNAME --password-stdin
docker push YOUR_USERNAME/myapp:1.0If the token is Read & Write, the push succeeds, or reports that the layers already exist. If you meant to make it read-only, the push should fail with denied. Seeing that failure confirms the restriction works.
Summary of habits
- Use a token, not your password, for every command-line login.
- Pick the lowest access level that works.
- Use one token per machine or pipeline.
- Feed tokens through
--password-stdin, never-p. - Prefer a credential helper over plaintext in
config.json. - Revoke at once if a token leaks, then rotate on a schedule.
Exercise
A deployment server only needs to run docker pull for your private images. Which access level should its token have, and how should the server log in non-interactively?
Show answer
Give it a Read-only token, so a compromise of the server cannot push or overwrite images. Store the token in an environment variable or secret store and log in with echo "$DOCKERHUB_TOKEN" | docker login -u YOUR_USERNAME --password-stdin. Do not use -p, because the value would be visible in process listings and shell history.
Exercise
You run docker login with a token and see Login Succeeded, but docker push fails with denied: requested access to the resource is denied. Give two possible causes and how to tell them apart.
Show answer
The token may be read-only, or the image name may not start with your namespace (for example myapp:1.0 instead of YOUR_USERNAME/myapp:1.0). Run docker images to check the name. If it already has your namespace, check the token's access level in Docker Hub and create a Read & Write token if needed.
Exercise
You find that a token was committed to a public repository an hour ago. List the steps you take, in order.
Show answer
First revoke the token in Docker Hub, because deleting the commit does not remove copies from history, forks, or scanners. Then create a replacement token, update the CI secret or credential store, and confirm a login works. Finally, remove the token from the repository and its history.