docker version: add "context" to output

This adds the currently selected "docker context" to the output
of "docker version", which allows users to see which context
is selected to produce the version output, and can be used (for
example), to set the prompt to the currently selected context:

(in `~/.bashrc`):

```bash
function docker_context_prompt() {
        PS1="context: $(docker version --format='{{.Client.Context}}')> "
}

PROMPT_COMMAND=docker_context_prompt
```

After reloading the `~/.bashrc`, the prompt now shows the currently selected
`docker context`:

```bash
$ source ~/.bashrc
context: default> docker context create --docker host=unix:///var/run/docker.sock my-context
my-context
Successfully created context "my-context"
context: default> docker context use my-context
my-context
Current context is now "my-context"
context: my-context> docker context use default
default
Current context is now "default"
context: default>
```

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-05-07 12:57:20 +02:00
parent 4f058143c7
commit 181e60499f
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 50 additions and 0 deletions

View File

@ -5,6 +5,7 @@ Client:
Git commit: deadbeef Git commit: deadbeef
Built: Wed May 30 22:21:05 2018 Built: Wed May 30 22:21:05 2018
OS/Arch: linux/amd64 OS/Arch: linux/amd64
Context: my-context
Experimental: true Experimental: true
Server: Docker Enterprise Edition (EE) 2.0 Server: Docker Enterprise Edition (EE) 2.0

View File

@ -32,6 +32,7 @@ Client:{{if ne .Platform.Name ""}} {{.Platform.Name}}{{end}}
Git commit: {{.GitCommit}} Git commit: {{.GitCommit}}
Built: {{.BuildTime}} Built: {{.BuildTime}}
OS/Arch: {{.Os}}/{{.Arch}} OS/Arch: {{.Os}}/{{.Arch}}
Context: {{.Context}}
Experimental: {{.Experimental}} Experimental: {{.Experimental}}
{{- end}} {{- end}}
@ -80,6 +81,7 @@ type clientVersion struct {
Os string Os string
Arch string Arch string
BuildTime string `json:",omitempty"` BuildTime string `json:",omitempty"`
Context string
Experimental bool Experimental bool
} }
@ -147,6 +149,7 @@ func runVersion(dockerCli command.Cli, opts *versionOptions) error {
Os: runtime.GOOS, Os: runtime.GOOS,
Arch: runtime.GOARCH, Arch: runtime.GOARCH,
Experimental: dockerCli.ClientInfo().HasExperimental, Experimental: dockerCli.ClientInfo().HasExperimental,
Context: dockerCli.CurrentContext(),
}, },
} }

View File

@ -41,6 +41,7 @@ func TestVersionAlign(t *testing.T) {
Os: "linux", Os: "linux",
Arch: "amd64", Arch: "amd64",
BuildTime: "Wed May 30 22:21:05 2018", BuildTime: "Wed May 30 22:21:05 2018",
Context: "my-context",
Experimental: true, Experimental: true,
}, },
Server: &types.Version{}, Server: &types.Version{},

View File

@ -39,6 +39,7 @@ Client:
Git commit: afacb8b Git commit: afacb8b
Built: Wed Mar 11 01:21:11 2020 Built: Wed Mar 11 01:21:11 2020
OS/Arch: darwin/amd64 OS/Arch: darwin/amd64
Context: default
Experimental: true Experimental: true
Server: Server:
@ -76,3 +77,47 @@ $ docker version --format '{{json .}}'
{"Client":{"Platform":{"Name":"Docker Engine - Community"},"Version":"19.03.8","ApiVersion":"1.40","DefaultAPIVersion":"1.40","GitCommit":"afacb8b","GoVersion":"go1.12.17","Os":"darwin","Arch":"amd64","BuildTime":"Wed Mar 11 01:21:11 2020","Experimental":true},"Server":{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"19.03.8","Details":{"ApiVersion":"1.40","Arch":"amd64","BuildTime":"Wed Mar 11 01:29:16 2020","Experimental":"true","GitCommit":"afacb8b","GoVersion":"go1.12.17","KernelVersion":"4.19.76-linuxkit","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"v1.2.13","Details":{"GitCommit":"7ad184331fa3e55e52b890ea95e65ba581ae3429"}},{"Name":"runc","Version":"1.0.0-rc10","Details":{"GitCommit":"dc9208a3303feef5b3839f4323d9beb36df0a9dd"}},{"Name":"docker-init","Version":"0.18.0","Details":{"GitCommit":"fec3683"}}],"Version":"19.03.8","ApiVersion":"1.40","MinAPIVersion":"1.12","GitCommit":"afacb8b","GoVersion":"go1.12.17","Os":"linux","Arch":"amd64","KernelVersion":"4.19.76-linuxkit","Experimental":true,"BuildTime":"2020-03-11T01:29:16.000000000+00:00"}} {"Client":{"Platform":{"Name":"Docker Engine - Community"},"Version":"19.03.8","ApiVersion":"1.40","DefaultAPIVersion":"1.40","GitCommit":"afacb8b","GoVersion":"go1.12.17","Os":"darwin","Arch":"amd64","BuildTime":"Wed Mar 11 01:21:11 2020","Experimental":true},"Server":{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"19.03.8","Details":{"ApiVersion":"1.40","Arch":"amd64","BuildTime":"Wed Mar 11 01:29:16 2020","Experimental":"true","GitCommit":"afacb8b","GoVersion":"go1.12.17","KernelVersion":"4.19.76-linuxkit","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"v1.2.13","Details":{"GitCommit":"7ad184331fa3e55e52b890ea95e65ba581ae3429"}},{"Name":"runc","Version":"1.0.0-rc10","Details":{"GitCommit":"dc9208a3303feef5b3839f4323d9beb36df0a9dd"}},{"Name":"docker-init","Version":"0.18.0","Details":{"GitCommit":"fec3683"}}],"Version":"19.03.8","ApiVersion":"1.40","MinAPIVersion":"1.12","GitCommit":"afacb8b","GoVersion":"go1.12.17","Os":"linux","Arch":"amd64","KernelVersion":"4.19.76-linuxkit","Experimental":true,"BuildTime":"2020-03-11T01:29:16.000000000+00:00"}}
``` ```
### Print the current context
The following example prints the currently used [`docker context`](context.md):
```bash
$ docker version --format='{{.Client.Context}}'
default
```
As an example, this output can be used to dynamically change your shell prompt
to indicate your active context. The example below illustrates how this output
could be used when using Bash as your shell.
Declare a function to obtain the current context in your `~/.bashrc`, and set
this command as your `PROMPT_COMMAND`
```bash
function docker_context_prompt() {
PS1="context: $(docker version --format='{{.Client.Context}}')> "
}
PROMPT_COMMAND=docker_context_prompt
```
After reloading the `~/.bashrc`, the prompt now shows the currently selected
`docker context`:
```bash
$ source ~/.bashrc
context: default> docker context create --docker host=unix:///var/run/docker.sock my-context
my-context
Successfully created context "my-context"
context: default> docker context use my-context
my-context
Current context is now "my-context"
context: my-context> docker context use default
default
Current context is now "default"
context: default>
```
Refer to the [`docker context` section](context.md) in the command line reference
for more information about `docker context`.