Browse topics

Selecting a Docker daemon with contexts

Version
29
Since
19.03
Review
Draft

Summary#

A Docker context names a daemon endpoint in the CLI configuration. In docker context ls, the asterisk marks the active context. Use docker context inspect to read the saved endpoint and docker info to check which daemon responds. Run docker --context NAME … to select a context for one invocation.

Syntax#

sh
docker context ls
docker context show
docker context inspect NAME
docker info --format '{{.Name}} {{.ID}}'
docker --context NAME info --format '{{.Name}} {{.ID}}'

Example#

Run this with a local daemon reachable over a Unix socket. Set DOCKER_SOCKET if it is somewhere other than /var/run/docker.sock. A temporary CLI configuration holds the example context. The script points DOCKER_HOST at a missing socket, confirms that the unqualified call fails, and compares the daemon ID from the one-command override with the ID from the local socket.

sh
(
  set -eu
  unset DOCKER_CONTEXT DOCKER_HOST DOCKER_TLS DOCKER_TLS_VERIFY DOCKER_CERT_PATH
  DOCKER_CONFIG=$(mktemp -d)
  export DOCKER_CONFIG
  trap 'rm -rf -- "$DOCKER_CONFIG"' EXIT

  name=dlr-p1-docker-commands-local
  endpoint="unix://${DOCKER_SOCKET:-/var/run/docker.sock}"
  docker context create "$name" --docker "host=$endpoint" >/dev/null 2>&1
  docker context ls --format '{{.Name}}' | LC_ALL=C sort

  saved=$(docker context inspect --format '{{.Endpoints.docker.Host}}' "$name")
  [ "$saved" = "$endpoint" ]
  printf 'saved endpoint matches the local socket\n'
  local_id=$(docker --host "$endpoint" info --format '{{.ID}}')

  export DOCKER_HOST="unix://$DOCKER_CONFIG/missing.sock"
  if docker info >/dev/null 2>&1; then
    exit 1
  fi
  printf 'default endpoint is unreachable\n'

  selected_id=$(docker --context "$name" info --format '{{.ID}}')
  [ -n "$selected_id" ]
  [ "$selected_id" = "$local_id" ]
  printf 'one-command override reached the local daemon\n'
  printf 'current context after the override: %s\n' "$(docker context show)"
)

Expected output#

Text
default
dlr-p1-docker-commands-local
saved endpoint matches the local socket
default endpoint is unreachable
one-command override reached the local daemon
current context after the override: default

The socket path and daemon ID can vary. The script checks them and prints the results of the comparisons.

Common mistakes#

docker context use saves a default for any shell sharing the CLI configuration. Use the global --context option before the subcommand for a one-off operation. DOCKER_CONTEXT overrides the saved default and DOCKER_HOST. When DOCKER_HOST supplies the endpoint, the context name can still appear as default. Inspect the endpoint and check the server's response before running a command that changes resources.

Version applicability#

The Docker 19.03 CLI introduced contexts. The complete example uses docker context show, added in CLI 23.0, and was checked with Docker CLI and Engine 29.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0