CodeBlanks Logo Icon CodeBlanks

docker login

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.io

Secure 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 Succeeded

The 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

terminal
# buraya yaz

Sample solution

terminal
echo "$GHCR_TOKEN" | docker login ghcr.io -u ali --password-stdin
docker push ghcr.io/ali/api:1.0

Expected 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.

Question
echo "$TOKEN" | docker login -u ali ____
Answer
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

All lessons in Beginner