How to validate Docker login without Docker?

Viewed 211

I have a script that runs on clean Ubuntu machines and installs Docker in order to pull some images. However, since it requires the container registry's username/password for docker login before pulling, I want to validate that the authentication will work before I proceed with the Docker installation, so the user wouldn't have to run the script twice in case they got the username/password wrong the first time.

Is there a way to verify the login without having Docker installed somehow? The script is running on clean Ubuntu machines so I'm very limited (Bash only).

2 Answers

Given that you have a login and a password/token, you can try this:

USERNAME=john@example.com
PASSWORD=foobar
AUTH=$(echo "$USERNAME:$PASSWORD" | base64)
curl -XGET --fail -H "Authorization: Basic $AUTH" \ 
  https://my.docker.registry.com/v2/[read_below] \
  >/dev/null 2>&1 && echo success!

For the [read_below] you have two options, choose one that works for your registry:

  • repository_name/image_name/tags/list
  • _catalog

curl will exit with non-zero code on any response except 200.

Your script could have a file for example environment.sh for variables, and the user running the script needs to enter the details and save before running the script, when the script runs the file with the variables loaded first to add the variables, this is also temporary for the bash session only. When the username and password is required for the registry, your variables can be used to login, and then carry on with the commands in your script.

Related