docker login
- Docker & Kubernetes course
- Beginner
- Lesson 67/82
- 4 min
Learn to log in to a registry with docker login.
docker login
docker login authenticates to a registry; if no address is given it connects to Docker Hub. Using an access token instead of a password is recommended: its scope is limited and it can be revoked on its own:
docker login # Docker Hub, interactive
docker login ghcr.io -u ali # another registry
docker logout ghcr.ioSecure login
Piping the token from an environment variable to standard input keeps it off the command line:
echo "$GHCR_TOKEN" | docker login ghcr.io -u ali --password-stdin
Login SucceededThe credentials are saved to ~/.docker/config.json or, if configured, the operating system's credential store. On shared machines, docker logout is done when finished.
Practice Logging in on CI
Write two commands: log in to ghcr.io as user ali by piping the $GHCR_TOKEN variable to standard input; then push ghcr.io/ali/api:1.0.
Starter code
# buraya yazSample solution
echo "$GHCR_TOKEN" | docker login ghcr.io -u ali --password-stdin
docker push ghcr.io/ali/api:1.0Expected output
Login Succeeded The push refers to repository [ghcr.io/ali/api] 1.0: digest: sha256:4a5b… size: 1789
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How should the password be given to docker login in a CI pipeline?
- On the command line with -p
- By writing it in the Dockerfile
- By adding it to the image tag
- From standard input with --password-stdin Correct answer
Explanation: A password given with -p shows up in the process list and shell history.
2 Read the password from standard input.
echo "$TOKEN" | docker login -u ali ____
echo "$TOKEN" | docker login -u ali --password-stdin
Explanation: --password-stdin takes the password from the pipe.
3 Match.
| Term | Meaning |
|---|---|
docker login |
Authenticates |
docker logout |
Removes credentials |
access token |
Limited, revocable |
config.json |
Credential record |