Sub topic 13
Logging in to a registry
This chapter assumes you can run docker build and docker push and that you know what a fully qualified image reference looks like (registry, repository, tag). It covers how Docker proves who you are to a registry, where it stores that proof, and how to fix the usual failures.
Why a registry asks who you are
A registry decides two things for each request: who is asking, and what that identity may do. Pulling a public image usually needs no credentials. Pushing almost always does. Private repositories require credentials for pulls as well.
Authentication (proving identity) is separate from authorization (what the identity may do). A successful login does not guarantee you can push to every repository. The registry checks permissions again for each repository you touch.
The basic login
docker login registry.example.comDocker prompts for a username and password, sends them to the registry, and saves the result locally if the registry accepts them. If you omit the hostname, Docker logs in to Docker Hub.
Avoid typing a secret as a command-line argument. Anything passed with --password can end up in your shell history and in the process list. Use --password-stdin instead:
echo "$REGISTRY_TOKEN" | docker login registry.example.com --username ci-bot --password-stdinThis is the form to use in scripts and CI jobs. The secret comes from an environment variable or a secret store and is never written into a file in your repository.
Use access tokens, not account passwords
Most registries let you create a personal access token or a service account key with limited scope. Prefer these over your main account password:
- You can restrict a token to read-only or to specific repositories.
- You can revoke a token without changing your account password.
- A leaked token has a smaller blast radius.
What happens behind the login
Docker does not send your password with every request. Registries that implement the Docker token authentication scheme work like this:
- The client sends a request, such as a push, to the registry without credentials.
- The registry replies
401 Unauthorizedwith aWWW-Authenticateheader. The header names an authentication service (the realm) and the scope being requested. - The client contacts that service with your stored credentials and asks for a token for that scope.
- The service checks permissions and returns a short-lived bearer token.
- The client repeats the original request with the token in an
Authorizationheader.
A scope looks like repository:team/myapp:pull,push. Because tokens are issued per scope, a login can succeed while a push to a repository you lack rights on is still refused.
Some registries use plain HTTP basic authentication instead. The client then sends your credentials directly with each request. The docker login command is the same either way.
Where Docker stores your credentials
By default, Docker writes the login to ~/.docker/config.json:
{
"auths": {
"registry.example.com": {
"auth": "Y2ktYm90OnMzY3IzdA=="
}
}
}The auth value is the string username:password encoded in base64. Base64 is an encoding, not encryption. Anyone who can read this file can decode your credentials with one command.
Credential helpers
A credential helper is a separate program that stores secrets in your operating system's keychain instead of in the file. Docker Desktop configures one for you. You enable a helper with the credsStore key, or set one per registry with credHelpers:
{
"credsStore": "desktop",
"credHelpers": {
"123456789012.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
}
}With a helper in place, the auths entry holds no secret, and Docker asks the helper for the credential when it needs one. The helper name maps to an executable called docker-credential-<name> on your PATH. If the executable is missing, Docker fails with an error mentioning that helper.
| Storage | Where the secret lives | Typical use |
|---|---|---|
| Default `auths` entry | Base64 text in `config.json` | Quick local tests, CI containers that are discarded |
| `credsStore` | OS keychain or credential manager | Developer laptops |
| `credHelpers` | Helper chosen per registry | Cloud registries with their own token tools |
Cloud registries and expiring credentials
Cloud providers often generate a temporary password instead of a long-lived one. Amazon ECR is a common example. You ask the AWS CLI for a password that is valid for 12 hours and pipe it into docker login:
aws ecr get-login-password --region us-east-1 \
| docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.comWhen that password expires, pushes start failing with unauthorized even though nothing in your setup changed. Run the login again. In CI, do the login at the start of every job, so a stale token never carries over.
Logging out and checking state
docker logout registry.example.comThis removes the stored credential for that registry. It does not revoke the token on the server. If a token leaked, revoke it in the registry's settings as well.
Docker has no built-in command that lists your logins. Open ~/.docker/config.json to see which registries have entries. With a credential helper, the file lists the registry names but not the secrets.
Local registries and plain HTTP
Docker treats localhost registries as safe and allows plain HTTP for them. The registry:2 container from the earlier chapter runs without authentication by default, so docker push localhost:5000/myapp:1.0 works with no login. For any other host, Docker expects HTTPS. Pointing it at an HTTP-only registry gives an error like http: server gave HTTP response to HTTPS client. The fix is to put TLS in front of the registry. Marking the host as an insecure registry in the daemon configuration also works, but only for isolated test setups.
Common errors
| Message | Likely cause | Fix |
|---|---|---|
| `unauthorized: authentication required` | Not logged in, or token expired | Run `docker login` again |
| `denied: requested access to the resource is denied` | Logged in, but no push rights, or wrong repository path | Check your permissions and the namespace in the image name |
| `Error saving credentials: ... docker-credential-...` | Configured helper is missing or failing | Install the helper or remove `credsStore` from `config.json` |
| `x509: certificate signed by unknown authority` | Registry uses a private CA | Install the CA certificate on the Docker host |
The denied error confuses many people because it appears after a successful login. Check the image name first. Pushing myapp:1.0 to Docker Hub tries to write to library/myapp, which you do not own. The name must start with your own namespace, such as youruser/myapp:1.0.
Exercise
You run docker login registry.example.com and it succeeds. You then run docker push registry.example.com/other-team/api:2.0 and get denied: requested access to the resource is denied. Give two possible causes and say which command's output you would check first.
Show answer
Possible causes: your account has no push permission on the other-team/api repository, or the repository path or namespace is wrong. The login succeeded, so your identity is valid, and the registry is refusing the scope for that repository. Check the registry's repository permissions and the exact image name you used. Running docker login again will not help.
Exercise
Why is echo "$TOKEN" | docker login --username ci-bot --password-stdin registry.example.com preferred over docker login --username ci-bot --password "$TOKEN" registry.example.com in CI scripts, and why is the auth value in config.json not a secure way to store the token?
Show answer
With --password, the token appears in the command line, so it can show up in shell history and in process listings. --password-stdin keeps it out of both. The auth value is only base64, which anyone can decode. A credential helper that uses the OS keychain keeps the secret out of the file.