`docker context use` shared between terminal sessions
Description
Setting a Docker context with docker context use shares the used context between terminal sessions. This is quite dangerous IMHO and almost made me to mess up a production environment since the old behaviour attached the context exclusively to the current session.
I guess this was rolled out with a new version, sure; but, since I am now paranoid and repulsive with using contexts, is there any possibility to disable this behaviour (have a context stick to the current terminal session like it was before)?
Steps to reproduce the issue:
-
docker context use chocolate - Open new terminal (window, tab or even a new terminal app like iTerm2 vs Terminal.app)
-
docker context showprintschocolate
Describe the results you received:
Opening a new terminal and doing docker context show prints chocolate.
Describe the results you expected:
Opening a new terminal and doing docker context show prints default.
Additional information you deem important (e.g. issue happens only occasionally):
Happens consistently. And quite dangerous IMHO, simply because you might have forgotten you set a production environment context and did a docker compose down -v.
Output of docker version:
Client: Docker Engine - Community
Cloud integration: 1.0.12
Version: 20.10.5
API version: 1.41
Go version: go1.13.15
Git commit: 55c4c88
Built: Tue Mar 2 20:13:00 2021
OS/Arch: darwin/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.5
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 363e9a8
Built: Tue Mar 2 20:15:47 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.4
GitCommit: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
runc:
Version: 1.0.0-rc93
GitCommit: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Output of docker info:
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
scan: Docker Scan (Docker Inc., v0.6.0)
Server:
Containers: 9
Running: 2
Paused: 0
Stopped: 7
Images: 67
Server Version: 20.10.5
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
Default Runtime: runc
Init Binary: docker-init
containerd version: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
runc version: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 5.10.25-linuxkit
Operating System: Docker Desktop
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 1.94GiB
Name: docker-desktop
ID: OFMV:OP7D:3KQH:Z7SF:4XNY:DXTV:XW5P:NG6T:Y7ZN:XFWX:YAZN:QV6J
Docker Root Dir: /var/lib/docker
Debug Mode: false
HTTP Proxy: http.docker.internal:3128
HTTPS Proxy: http.docker.internal:3128
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Additional environment details (AWS, VirtualBox, physical, etc.):
Just my Mac.
This is the expected behaviour for docker context use, as it configures the default context for the CLI to use. To switch context in a specific shell, or for a single command, you can use the DOCKER_CONTEXT environment variable instead.
That said, I think it makes sense to more explicitly mention this (and to show the alternative uses).
Awesome! Doing export DOCKER_CONTEXT=chocolate makes sense. Thanks!
Note that with docker 20.10, we added the active context to the output of docker version, which could be used to (e.g.) include it in your prompt; see https://github.com/docker/cli/pull/2500 (note that docker version will still make a connection with the daemon, so it may still not be ideal, but perhaps it's useful for your scenario)
Docs now mention the docker context show command; https://docs.docker.com/reference/cli/docker/context/show/
We probably should still tweak the documentation for docker context use to;
- Describe that it's updating the CLI's configuration file (so change is "sticky")
- Describe that this affects all terminals (due to it being a configuration change)
- Show some examples with
DOCKER_CONTEXT=xxxand--contextto illustrate those can be used to temporarily switch, or to change the context for a specific shell (export DOCKER_CONTEXT=xxx...)