LinuxCommandLibrary

docker-context

Manage Docker connection configurations

TLDR

Create a context using a specific Docker endpoint

$ docker context create [context_name] --docker "host=[tcp://remote-host:2375]"
copy

Create a context based on the $DOCKER_HOST environment variable
$ docker context create [context_name]
copy

Switch to a context
$ docker context use [context_name]
copy

List all contexts
$ docker context ls
copy

SYNOPSIS

docker context COMMAND [OPTIONS]
where COMMAND = create|export|inspect|ls|rm|use

PARAMETERS

create
    Create a new context with specified name

--description
    Add a description to the context (create)

--docker host=
    Set Docker endpoint URI (tcp://, ssh://, npipe://); supports TLS flags (create)

--docker tls
    Enable TLS for Docker endpoint (create)

--docker tlsverify
    Enable TLS verification for Docker (create)

--docker tlscacert=
    CA certificate path for Docker TLS (create)

--docker tlscert=
    Client certificate path for Docker TLS (create)

--docker tlskey=
    Client key path for Docker TLS (create)

--kubernetes config-from=
    Path to kubeconfig; optional context name (create)

--kubeconfig=


    Directory for contexts meta (all subcommands)

export [|-]
    Export context as JSON to stdout if -

inspect
    Display detailed context info

-f, --format
    Pretty-print format with Go template (inspect, ls)

ls [-q]
    List contexts; -q for quiet name-only

rm
    Remove a context

use
    Set default context for current session

DESCRIPTION

The docker context command manages contexts in the Docker CLI, enabling seamless switching between different Docker hosts, Kubernetes clusters, and authentication settings. A context groups an endpoint (Docker daemon or Kubernetes API server), credentials, and metadata, stored in ~/.docker/contexts.json by default.

This is useful for developers working across local Docker Desktop, remote servers, cloud providers, or hybrid Docker/Kubernetes setups. Instead of editing config files or environment variables, use docker context use to switch instantly. Contexts support Docker via TCP, SSH, or named pipes, and Kubernetes via kubeconfig files.

Key benefits include reproducibility, team sharing via exported JSON, and avoiding context pollution in shared environments. It's part of modern Docker workflows, complementing Compose and Swarm.

CAVEATS

Current context changes affect only the CLI session; Docker daemon must be accessible. Kubernetes contexts require valid kubeconfig. Default context is 'default'. Cannot rm active context.

EXAMPLE USAGE

docker context create prod --docker host=tcp://remote:2376 --docker tlsverify --docker tlscacert=/path/ca.pem
docker context use prod
docker context ls

STORAGE LOCATION

Contexts stored as JSON in $DOCKER_CONTEXT_DIR/contexts/meta.json or ~/.docker/. Use DOCKER_CONTEXT_DIR env var to override.

HISTORY

Introduced in Docker CLI v19.03.0 (March 2019) to address multi-environment pain points. Evolved with Docker 20+ for better Kubernetes integration and export features. Now standard in Docker Desktop and CLI.

SEE ALSO

docker(1), kubectl config(1)

Copied to clipboard