When you operate more than one Docker host — dev laptop, staging VM, production node — typing ssh prod-host docker ps every five minutes gets old fast. Docker contexts let your local docker CLI talk to remote daemons as if they were local, with a single command to switch between them. Done right, contexts make it impossible to confuse environments because the current context shows up in your prompt and on every command.
How to verify
Check what contexts exist and which one is active:
docker context ls
docker context show # current context only
docker context inspect default
# Verify the active daemon
docker info | grep -E 'Name|Server Version'
# A quick canary to confirm you're talking to the right host
docker run --rm alpine hostname
The default context (default) talks to the local daemon over the Unix socket. Any context you add points elsewhere — over SSH most commonly, or over TCP+TLS for the more involved setups.
What’s happening
A Docker context is a named bundle of three things: an endpoint (where the daemon lives), credentials (how to authenticate), and metadata (which orchestrator to assume). The CLI consults the active context for every command. When the active context is prod, docker ps is equivalent to ssh prod docker ps — but the streams, exit codes, and TTY handling all work as if local.
There are two transports worth knowing:
- SSH — the path of least resistance. The CLI tunnels Docker API calls over an SSH connection to the remote host, where the daemon serves its Unix socket. No daemon-side TLS config, no exposed ports, and SSH handles authentication. Requires
dockerinstalled on the remote host (the SSH transport invokes it as a helper). - TCP+TLS — the daemon listens on a TCP port (2376 by convention) and validates client certificates. More work to set up but doesn’t require an SSH login on the remote. Right answer for CI runners and read-only dashboards. See TLS daemon for remote Docker access for the full setup.
Contexts persist across CLI sessions in ~/.docker/contexts/meta/. They’re machine-local — they don’t sync between your laptop and your team.
The procedure
- Add a context that points at a remote host over SSH. The user must have SSH access and permission to run
dockeron the remote (usually via membership in thedockergroup):
docker context create prod \
--description "Production cluster" \
--docker "host=ssh://ops@prod-01.example.com"
docker context ls
The output now shows two contexts: default (your laptop) and prod (the remote host).
- Switch to it and run a command. The active context applies to every subsequent docker call until you switch back or use the override flag:
docker context use prod
docker ps # runs on prod-01
docker exec -it web bash # exec into a container on prod-01
# One-shot override without switching the active context
docker --context prod ps
docker -c default ps # short form
- Set up your shell prompt to display the active context so you can never confuse environments. For bash:
# in ~/.bashrc
docker_context() {
local ctx=$(docker context show 2>/dev/null)
if [ "$ctx" != "default" ] && [ -n "$ctx" ]; then
echo " [$ctx]"
fi
}
PS1='\u@\h:\w$(docker_context)\$ '
Now your prompt reads you@laptop:~ [prod]$ whenever a non-default context is active. Cheap, ugly, effective.
- Use environment variables to scope a context to a single shell session, instead of mutating the global active context:
export DOCKER_CONTEXT=prod
docker ps # uses prod
# new terminal: no DOCKER_CONTEXT set, uses default
This is the safer pattern for terminal-multiplexer workflows (tmux, screen) — each pane has its own scope and there’s no risk of an old context lingering when you wake the laptop tomorrow.
- Pair contexts with Compose for multi-host operations:
docker --context staging compose -f docker-compose.staging.yml up -d
docker --context prod compose -f docker-compose.prod.yml pull
Compose v2 honours --context at the docker level, so the whole stack runs against the chosen daemon.
- Export and import contexts when onboarding new operators. You don’t have to re-type the connection details on every laptop:
# Export (creates context.tar.gz with config and any SSH known_hosts pinning)
docker context export prod
# Import on the other machine
docker context import prod prod.dockercontext
- Remove a context when the host is decommissioned. Switch off it first or Docker refuses to delete the active one:
docker context use default
docker context rm staging
Common pitfalls
- The SSH transport requires
dockeron the remote host — not justdockerd. If the remote was set up with only the daemon and no CLI, installdocker-ce-cli. docker composev2 honours contexts; the legacydocker-composePython script does not. If your compose commands ignore--context, you’re on the old binary — uninstall it and use the plugin.- A context that points at a host you’ve SSH-keyed but never logged into will fail with a host key verification error. SSH into the host once interactively first, accept the fingerprint, then the context works.
- Switching contexts does not move volumes, networks, or images.
docker volume lsshows volumes on the current host. There’s no built-in cross-context resource view — that’s by design. - Build commands with BuildKit’s
docker buildx buildmay use a builder bound to a different context than the active one. Rundocker buildx lsto see which builder is current anddocker buildx use --default <name>to align them.
We provision contexts on every operator laptop we hand off, with separate names per environment and shell-prompt hooks that make the active host impossible to miss. It removes a whole category of wrong-cluster mistakes and is one of the small habits underneath our managed operations practice.