Sub topic 4
Logging in: docker login and credential helpers
This chapter assumes you can build and run images with Docker and use a terminal. It also assumes you know the parts of an image reference (host, repository, tag). You will learn what docker login does, where Docker keeps your credentials, and how credential helpers store them more safely.
What docker login does
A private registry only accepts requests from clients that prove who they are. docker login sends a username and password (or token) to the registry. If the registry accepts them, Docker saves them and sends them with later push and pull commands for that host.
echo "$REGISTRY_TOKEN" | docker login registry.example.com -u your-username --password-stdinUse --password-stdin instead of -p. A password passed with -p ends up in your shell history and in the process list. Docker also warns you when you use it.
If you leave out the host, Docker logs you in to Docker Hub. Credentials are stored per registry host. Logging in to ghcr.io does not give you access to any other registry.
Where the credentials go
Docker keeps its client settings in ~/.docker/config.json. By default, docker login writes your credentials there under auths:
{
"auths": {
"registry.example.com": {
"auth": "dXNlcjpwYXNzd29yZA=="
}
}
}The auth value is only the base64 encoding of username:password. Anyone who can read the file can decode it:
echo "dXNlcjpwYXNzd29yZA==" | base64 -dCredential helpers
A credential helper is a small program that stores and returns credentials on Docker's behalf. Docker finds the helper by name: for a helper called osxkeychain, it runs docker-credential-osxkeychain. Common helpers use the operating system's secure store:
| Helper | Platform |
|---|---|
| `osxkeychain` | macOS Keychain |
| `wincred` | Windows Credential Manager |
| `secretservice` | Linux Secret Service (GNOME Keyring) |
| `pass` | Linux, using the `pass` password manager |
You turn on a helper in config.json. There are two settings:
credsStoresets one helper for every registry.credHelperssets a helper for individual registry hosts.
{
"credsStore": "osxkeychain",
"credHelpers": {
"123456789012.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
}
}A credHelpers entry takes priority over credsStore for that host. The ecr-login helper is a special case. It does not store a password. It asks AWS for a fresh temporary token each time Docker needs one, so nothing long-lived sits on disk. The ECR chapter covers it.
After you set a helper, run docker login again. Docker then hands the credentials to the helper, and config.json keeps only an empty entry for the host.
Logging out and common problems
docker logout registry.example.com removes the saved credentials for that host.
docker-credential-xyz: executable file not found:config.jsonnames a helper that is not installed or not on yourPATH. Install it, or remove the setting.- Login works, push says
denied: The login only proved your identity. The account may lack permission on that repository, which later chapters cover. - Login in CI fails on a headless Linux machine: A keychain-based helper needs a desktop session. In CI, use a short-lived token and
--password-stdin, and discard the runner afterward.
Exercise
Your config.json contains "credsStore": "osxkeychain" and "credHelpers": {"ghcr.io": "pass"}. Which helper does Docker use for ghcr.io, and why should you avoid docker login -p?
Show answer
Docker uses pass for ghcr.io, because a credHelpers entry overrides credsStore for that host. It uses osxkeychain for all other hosts. -p places the password on the command line, where it is saved in shell history and visible in the process list. --password-stdin avoids both.