Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if docker is already logged in to a docker registry server

Tags:

docker

People also ask

Is docker registry and Docker Hub same?

Docker registries are used to host and distribute Docker Images. Docker Hub is Docker's official cloud-based registry. To get started with Docker Hub you can pull (download) an image or push (upload) one of your local images.

Can I login to multiple docker registries?

With Codefresh you can connect multiple registries on a global level. This allows you to create pipelines that push/pull images to different registries without having to deal with Docker credentials inside the pipeline itself.

How does a docker registry work?

A Docker registry is organized into Docker repositories , where a repository holds all the versions of a specific image. The registry allows Docker users to pull images locally, as well as push new images to the registry (given adequate access permissions when applicable).


Edit 2020

Referring back to the (closed) github issue, where it is pointed out, there is no actual session or state;

docker login actually isn't creating any sort of persistent session, it is only storing the user's credentials on disk so that when authentication is required it can read them to login

As others have pointed out, an auths entry/node is added to the ~/.docker/config.json file (this also works for private registries) after you succesfully login:

{
    "auths": {
            "https://index.docker.io/v1/": {}
    },
    ...

When logging out, this entry is then removed:

$ docker logout
Removing login credentials for https://index.docker.io/v1/

Content of docker config.json after:

{
    "auths": {},
    ...

This file can be parsed by your script or code to check your login status.

Alternative method (re-login)

You can login to docker with docker login <repository>

$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If 
you don't have a Docker ID, head over to https://hub.docker.com to 
create one.
Username:

If you are already logged in, the prompt will look like:

$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If 
you don't have a Docker ID, head over to https://hub.docker.com to 
create one.
Username (myusername):        # <-- "myusername"

For the original explanation for the ~/.docker/config.json, check question: how can I tell if I'm logged into a private docker registry


I use one of the following two ways for this check:

1: View config.json file:

In case you are logged in to "private.registry.com" you will see an entry for the same as following in ~/.docker/config.json:

"auths": {
    "private.registry.com": {
        "auth": "gibberishgibberishgibberishgibberishgibberishgibberish"
    }
 }

2: Try docker login once again:

If you are trying to see if you already have an active session with private.registry.com, try to login again:

bash$ docker login private.registry.com
Username (logged-in-user):

If you get an output like the above, it means logged-in-user already had an active session with private.registry.com. If you are just prompted for username instead, that would indicate that there's no active session.


You can do the following command to see the username you are logged in with and the registry used:

docker system info | grep -E 'Username|Registry'

The answers here so far are not so useful:

  • docker info no longer provides this info
  • docker logout is a major inconvenience - unless you already know the credentials and can easily re-login
  • docker login response seems quite unreliable and not so easy to parse by the program

My solution that worked for me builds on @noobuntu's comment: I figured that if I already known the image that I want to pull, but I'm not sure if the user is already logged in, I can do this:

try pulling target image
-> on failure:
   try logging in
   -> on failure: throw CannotLogInException
   -> on success:
      try pulling target image
      -> on failure: throw CannotPullImageException
      -> on success: (continue)
-> on success: (continue)

The docker cli credential scheme is unsurprisingly uncomplicated, just take a look:

cat ~/.docker/config.json

{
  "auths": {
    "dockerregistry.myregistry.com": {},
    "https://index.docker.io/v1/": {}

This exists on Windows (use Get-Content ~\.docker\config.json) and you can also poke around the credential tool which also lists the username ... and I think you can even retrieve the password

. "C:\Program Files\Docker\Docker\resources\bin\docker-credential-wincred.exe" list

{"https://index.docker.io/v1/":"kcd"}

At least in "Docker for Windows" you can see if you are logged in to docker hub over the UI. Just right click the docker icon in the windows notification area: Docker Logged in


If you want a simple true/false value, you can pipe your docker.json to jq.

is_logged_in() {
  cat ~/.docker/config.json | jq -r --arg url "${REPOSITORY_URL}" '.auths | has($url)'
}

if [[ "$(is_logged_in)" == "false" ]]; then
  # do stuff, log in
fi