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

后端 未结 13 2489
后悔当初
后悔当初 2020-12-23 02:49

I\'m not sure if I have already logged in to a docker registry in cmd line by using cmd: docker login. How can you test or see whether you are logged in or not, without tryi

13条回答
  •  一向
    一向 (楼主)
    2020-12-23 03:19

    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

    $ 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

提交回复
热议问题