How can I tell if I'm logged in to a private D

2019-01-25 03:26发布

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 successfully?

Note: I'm asking about an arbitrary private registry, not the docker.io registry.

标签: docker
3条回答
霸刀☆藐视天下
2楼-- · 2019-01-25 03:47

This is a bit hacky, but it works in most of the cases for me:

if ! grep -q "my.private.registry.com" ~/.docker/config.json ; then
    docker login "my.private.registry.com"
fi

Basically, you search if there is a record of "my.private.registry.com" in ~/.docker/config.json. However, if the session is expired, this check won't catch it.

查看更多
Evening l夕情丶
3楼-- · 2019-01-25 03:51

if docker login worked, you will find a .docker folder on your home directory (~/.docker/), with a config.json file with credentials in it.

otherwise you would get an error login in.

Note: docker determine what credentials to use by looking at the registry name:

if you do

docker pull myregistry.com/myimage:tag

docker will look if you're logged in, and if not will check if you have the credentials for the registry myregistry.com and login with those.

If not you will get a permission error

查看更多
可以哭但决不认输i
4楼-- · 2019-01-25 04:01

When you do the

Docker login <private registry> -u <user> -p <password> 

command from your terminal, you will have a response: (stored in $?)

0
Login Succeeded

if you were successful.

In your shell script, you could just look at the response you're receiving, if it does not equal 0, you've failed to login.

sudo docker login <registry> -u <uname> -p <password>
if [ $? -ne 0 ]; then
    echo Login failed!
else
    echo Login OK!
fi
查看更多
登录 后发表回答