Docker login/logout command
docker login and docker logout The commands are used to manage authentication between the Docker client and the Docker registry. Through the docker login command, users can log in to a Docker registry, access and push images; through the docker logout command, users can log out.
-
docker login : Log in to a Docker image registry; if no registry address is specified, the default is the official registry, Docker Hub
-
docker logout : Log out of a Docker image registry; if no registry address is specified, the default is the official registry, Docker Hub
docker login
docker login The command is used to log in to a Docker registry.
By default, it logs in to Docker Hub, but you can also log in to other Docker registries.
Syntax
docker login [OPTIONS] [SERVER]
SERVER: The server address of the Docker registry (Docker Hub by default).
-u, --username: The login username.-p, --password: The login password (not recommended for use on the command line).--password-stdin: Read the password from standard input.
Usage examples
1. Log in to Docker Hub
docker login
Prompts you to enter your Docker Hub username and password.
** 2. Log in to Docker Hub (specify username)**
docker login --username myusername
3. Prompts you to enter the Docker Hub password.
Read the password from standard input:
echo "mypassword" | docker login --username myusername --password-stdin
Read the password from standard input to avoid entering it in plain text on the command line.
** 4. Log in to a custom registry**
docker login myregistry.com
Prompts you to enter the username and password for the custom registry.
docker logout
docker logout The command is used to log out of the currently logged-in Docker registry. By default, it logs out of Docker Hub, but you can specify another registry.
Syntax
docker logout [SERVER]
SERVER: The server address of the Docker registry (Docker Hub by default).
Usage examples
** 1. Log out of Docker Hub**
docker logout
Log out the current user from Docker Hub.
** 2. Log out of a custom registry**
docker logout myregistry.com
Log out the current user from myregistry.com.
Practical examples
1. Log in to Docker Hub
docker login
Output:
Username: myusername
Password:
Login Succeeded
2. Log in to Docker Hub with the password read from standard input
echo "mypassword" | docker login --username myusername --password-stdin
Output:
Login Succeeded
3. Log in to a custom registry
docker login myregistry.com
Output:
Username: myusername
Password:
Login Succeeded
4. Log out of Docker Hub
docker logout
Output:
Removing login credentials for https://index.docker.io/v1/
5. Log out of a custom registry
docker logout myregistry.com
Output:
Removing login credentials for myregistry.com
Notes
- It is not recommended to use the
-poption to enter a password on the command line, because this exposes the password in plain text in the command history. - Use
--password-stdinThis avoids plain-text password exposure and enhances security. - Make sure to execute the command in a public or shared environment
docker logoutto prevent other users from accessing your account.
Docker Command Reference