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 13:45

The docker cli credential scheme is unsurprisingly uncomplicated, just take a look:

cat ~\.docker\config.json

{
  "auths": {
    "dockerregistry.myregistry.com": {},
    "https://index.docker.io/v1/": {}

On Windows you can also poke around the credential tool which also lists the username, just to poke around too much ;)

. "C:\Program Files\Docker\Docker\resources\bin\docker-credential-wincred.exe" list

{"https://index.docker.io/v1/":"kcd"}
查看更多
ゆ 、 Hurt°
3楼-- · 2019-03-11 13:49

I use one of the following two ways for this check:

1: View config.json file:

In case you are logged in to "private.registry.com" you will see an entry for the same as following in ~/.docker/config.json:

"auths": {
    "private.registry.com": {
        "auth": "gibberishgibberishgibberishgibberishgibberishgibberish"
    }
 }

2: Try docker login once again:

If you are trying to see if you already have an active session with private.registry.com, try to login again:

bash$ docker login private.registry.com
Username (logged-in-user):

If you get an output like the above, it means logged-in-user already had an active session with private.registry.com. If you are just promoted for username instead, that would indicate that there's no active session.

查看更多
4楼-- · 2019-03-11 13:51

Edit

You can login to docker with docker login <repository>

$ 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):

Answer below is obsolete, docker info no longer shows username information.

Historic

When you are logged in, your username and registry shows up in the docker info command

Like this:

[ciccio@consolecowboy.net ~]$ docker info
Containers: 12
 Running: 2
..etc...
...
Username: francobolli
Registry: https://index.docker.io/v1/

Edit: Note; this only works for index.docker.io repo. Login only stores the credentials on disk when it needs them, for example when running docker pull localhost:5000/image or docker pull quay.io/username/reponame.

Edited after this got pointed out in the comments of this question: how can I tell if I'm logged into a private docker registry

查看更多
我想做一个坏孩纸
5楼-- · 2019-03-11 13:52

The answers here so far are not so useful:

  • docker info no longer provides this info
  • docker logout is a major inconvenience - unless you already know the credentials and can easily re-login
  • docker login response seems quite unreliable and not so easy to parse by the program

My solution that worked for me builds on @noobuntu's comment: I figured that if I already known the image that I want to pull, but I'm not sure if the user is already logged in, I can do this:

try pulling target image
-> on failure:
   try logging in
   -> on failure: throw CannotLogInException
   -> on success:
      try pulling target image
      -> on failure: throw CannotPullImageException
      -> on success: (continue)
-> on success: (continue)
查看更多
Rolldiameter
6楼-- · 2019-03-11 13:56

Just checked, today it looks like this:

$ docker login
Authenticating with existing credentials...
Login Succeeded

NOTE: this is on a macOS with the latest version of Docker CE, docker-credential-helper - both installed with homebrew.

查看更多
叼着烟拽天下
7楼-- · 2019-03-11 14:02

Use command like below:

docker info | grep 'name'

WARNING: No swap limit support
Username: <strong>jonasm2009</strong>
查看更多
登录 后发表回答