How to know if docker is already logged in to a do

2019-03-11 13:24发布

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 trying to push?

标签: docker
11条回答
女痞
2楼-- · 2019-03-11 14:03

For private registries, nothing is shown in docker info. However, the logout command will tell you if you were logged in:

 $ docker logout private.example.com
 Not logged in to private.example.com

(Though this will force you to log in again.)

查看更多
疯言疯语
3楼-- · 2019-03-11 14:08

On windows you can inspect the login "authorizations" (auths) by looking at this file: [USER_HOME_DIR].docker\config.json

Example: c:\USERS\YOUR_USERANME.docker\config.json

It will look something like this for windows credentials

{
"auths": {
    "HOST_NAME_HERE": {},
    "https://index.docker.io/v1/": {}
},
"HttpHeaders": {
    "User-Agent": "Docker-Client/18.09.0 (windows)"
},
"credsStore": "wincred",
"stackOrchestrator": "swarm"
}
查看更多
\"骚年 ilove
4楼-- · 2019-03-11 14:08

As pointed out by @Christian, best to try operation first then login only if necessary. Problem is that "if necessary" is not that obvious to do robustly. One approach is to compare the stderr of the docker operation with some strings that are known (by trial and error). For example,

try "docker OPERATION"
if it failed: 
    capture the stderr of "docker OPERATION"
    if it ends with "no basic auth credentials": 
        try docker login
    else if it ends with "not found":
        fatal error: image name/tag probably incorrect
    else if it ends with <other stuff you care to trap>:
        ...
    else:
        fatal error: unknown cause

try docker OPERATION again
if this fails: you're SOL!
查看更多
狗以群分
5楼-- · 2019-03-11 14:10

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
查看更多
Juvenile、少年°
6楼-- · 2019-03-11 14:11

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

查看更多
登录 后发表回答