Sub topic 13
Logging in to a registry
This chapter assumes you can build and tag images, know the registry/repository:tag naming format, and have Docker running locally. It covers how Docker authenticates to a registry, where it stores credentials, and the errors you are most likely to hit.
Why registries ask for credentials
Most registries let anyone pull public images but require authentication to push, and to pull private images. Docker sends your credentials to the registry, which replies with a token. The token is then attached to later push and pull requests.
Logging in
Run docker login with the registry hostname. If you leave the hostname out, Docker uses Docker Hub.
docker login registry.example.comDocker prompts for a username and a password or access token. Prefer an access token over your account password. A token can be limited to certain repositories and revoked on its own.
For scripts and CI, never put the secret on the command line. Command-line arguments can show up in shell history and process lists. Pipe it in on standard input instead:
echo "$REGISTRY_TOKEN" | docker login registry.example.com --username ci-bot --password-stdinLog out with docker logout registry.example.com. This removes the stored credential for that registry.
Where credentials are stored
Docker keeps login information in ~/.docker/config.json. What it saves depends on your setup.
| Setup | What is stored |
|---|---|
| No credential helper | A base64-encoded `username:secret` string in `config.json`. Base64 is an encoding, not encryption. |
| Credential helper configured | A pointer in `config.json`. The secret lives in the OS keychain or a secret manager. |
On Docker Desktop and many Linux setups a credential helper is used, so secrets stay out of the file. If you see an auth entry in config.json, treat the file like a password.
What happens on push
Many registries work this way, though details vary. Some accept the credentials directly with Basic authentication.
Common errors
denied: requested access to the resource is denied: you are not logged in, your account lacks write access, or the repository path is wrong. Check the name first, then the login.unauthorized: authentication required: no valid credential for that exact hostname. Credentials are stored per registry, so a login to Docker Hub does not apply toregistry.example.com.x509: certificate signed by unknown authority: the registry uses a certificate your machine does not trust. Install the correct CA certificate rather than disabling TLS.- Expired token: tokens from cloud registries often last only hours. Log in again.
Your local registry from the earlier chapter on localhost:5000 has no authentication by default, so docker login is not needed there.
Exercise
A CI job runs docker login registry.example.com -u bot -p "$TOKEN". Give two reasons this is a poor choice and show the better command.
Show answer
The -p flag exposes the token in the process list and possibly in logs or shell history, and Docker warns that it is insecure. Use echo "$TOKEN" | docker login registry.example.com -u bot --password-stdin, ideally with a short-lived, narrowly scoped token and a credential helper where available.