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

后端 未结 13 2477
后悔当初
后悔当初 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:03

    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 :
            ...
        else:
            fatal error: unknown cause
    
    try docker OPERATION again
    if this fails: you're SOL!
    

提交回复
热议问题