Log In to a Registry
docker login
docker login registry.example.com
docker login registry.example.com:5000
Without a server name, docker login signs in to the default registry, the one Docker uses when an image name has no registry part. With a name, it signs in to that registry only. Logins are stored per registry, so you can be signed in to several at once. Recent versions of the Docker CLI sign in to the default registry through the browser with a one-time code; add --username to get the classic prompt instead.
Pass the Secret on Standard Input
On a server, keep the token in a file that only you can read, then feed it to --password-stdin:
install -m 600 /dev/null ~/.registry-token
nano ~/.registry-token
docker login registry.example.com --username deploy --password-stdin < ~/.registry-token
Never use -p or --password. A secret given as an option is visible to other users in the process list and stays in your shell history. Docker prints a warning when you do it, and it is right to.
Use an Access Token, Not the Account Password
Most registries let you create access tokens or robot accounts with a limited scope. Use them:
- Least rights: a server that only pulls gets a read-only token; only the build system needs push rights.
- Quick to revoke: a leaked token is deleted without changing the account password or touching other machines.
- Two-factor accounts: registries that enforce two-factor login usually accept only tokens on the command line.
Where Docker Stores the Login
The result goes into ~/.docker/config.json of the user who ran the command; with sudo that is root's home. Without a credential helper, the entry looks like this:
{
"auths": {
"registry.example.com": {
"auth": "BASE64_OF_USER_AND_TOKEN"
}
}
}
The auth value is the user name and token joined by a colon and encoded with base64. Base64 is an encoding, not encryption: anyone who can read the file can decode it. Keep the file at mode 600, and consider a credential helper. With a credsStore or credHelpers entry in the same file, Docker hands the secret to a helper program instead, such as docker-credential-pass, which keeps it encrypted with pass and GPG on a server without a desktop.
Log Out
docker logout
docker logout registry.example.com
This removes the stored entry for that registry. Revoke the token in the registry as well when a server is retired.
Pull Limits for Anonymous Users
The default registry limits how many images an anonymous client may pull in a period, counted per IPv4 address or IPv6 subnet, and gives signed-in accounts a higher allowance. Servers that share one outgoing address share the limit. Logging in on each server, or running a pull-through cache, avoids surprises during deployments.
Logging In from Automation
- Secrets from the CI system: pipe the stored secret into the command, for example
printf '%s' "$REGISTRY_TOKEN" | docker login registry.example.com --username deploy --password-stdin. printf is a shell builtin, so the token does not appear in the process list. - A separate config per job: set
DOCKER_CONFIGto a temporary folder so one job's login does not stay on the runner, and delete the folder afterward. - Log out at the end of the job when the machine is shared.
Common Errors
- Unauthorized or incorrect user name or password: the token is wrong, expired, or lacks the scope; or you logged in to a different host name.
registry.example.comandregistry.example.com:5000are stored as separate entries. - Certificate signed by unknown authority: the registry uses a private CA. Place the CA certificate at
/etc/docker/certs.d/registry.example.com/ca.crt; if you reach the registry with a port number, the folder name includes it, as inregistry.example.com:5000. Marking the registry as insecure in the daemon settings switches off verification and is not a fix. - Cannot perform an interactive login from a non-TTY device: a script has no terminal for the prompt; use
--password-stdin. - Error saving credentials:
config.jsonnames a credential helper that is not installed. Install it, or remove thecredsStoreline.
After login, tag and push your own images as shown in docker tag. Kubernetes clusters can reuse the same file for private images, as described in ImagePullBackOff. To replace a running container with a newly pulled image, see how to update a Docker container.