Log in to Docker image repository
docker login is used to connect the local Docker client to the image repository. After successful login, docker pull and docker push will use the saved credentials to access private images or restricted resources.
Log in to Docker Hub
Direct execution:
bashdocker login
The new version of the Docker client can automatically use device codes for login on Docker Hub. The terminal will display a verification address and a one-time code, and authorization can be completed in a browser. If you need to explicitly specify an account, you can use:
bashdocker login --username <docker-id>
Log in to the self-built repository
In the command, only enter the repository host name and optional port; do not add the image path:
bashdocker login registry.example.com
docker login registry.example.com:5000
Using tokens in scripts
Automated scripts should not write passwords directly into the command line. Using --password-stdin can prevent passwords from appearing in the shell history and process list:
bashprintf '%s' "$REGISTRY_TOKEN" | \
docker login registry.example.com \
--username "$REGISTRY_USER" \
--password-stdin
REGISTRY_TOKEN should be injected by an interactive terminal, key management service, or continuous integration platform, and should not be written into tutorials, source code repositories, or image layers.
Credential Storage and Exit
Docker typically writes the login configuration to ~/.docker/config.json. If the system is configured with a credential store, the actual credentials are managed by the operating system keychain. Exit after using a public device or a short-term server:
bashdocker logout
docker logout registry.example.com
Verification and Debugging
bashdocker info
docker pull hello-world:latest
- When
unauthorizedappears, first check the repository address, username, and token permissions. - If you are logged in but still cannot push, verify that the image name contains the correct repository prefix and namespace.
- On servers without a graphical interface, if the keychain cannot be used, you can configure a available credential helper for the host, or use a short-term token with limited permissions.
Official reference: Docker CLI login.