How can I tell if I'm logged in to a private Docker registry from a script?

后端 未结 7 1282
粉色の甜心
粉色の甜心 2021-02-04 23:59

How can I tell whether or not I\'m logged in to a private Docker registry server from a script? In other words, has docker login some.registry.com been run success

7条回答
  •  忘掉有多难
    2021-02-05 00:34

    This is a little hacky, I think until docker will have a command to check login, there won't be any good solution.
    You can in your bash script try to login with timeout of x seconds, if you aren't logged in the command will try to prompt for username and then it will timeout with status 124. If you are indeed logged in, it will just log you in again using the save credentials and continue with status 0

    #!/bin/bash
    timeout -s SIGKILL 3s docker login some.registry.com >/dev/null 2>&1
    if [ $? -eq 0 ]
    then
       echo Logged In!
    else
       echo Not logged in...
    fi
    

提交回复
热议问题