Docker Context

Difficulty: Advanced

Question

What is a Docker context, and how do you use contexts to manage local and remote Docker hosts?

Answer

A Docker context is a named profile that tells the Docker CLI which daemon to talk to and how to authenticate, for example your laptop's local socket, a staging VM over SSH, or a production host over TLS. You switch with docker context use <name>, and every later docker and docker compose command runs against that daemon without changing any flags.

What a context stores

Each context holds an endpoint (host=unix:///var/run/docker.sock, host=ssh://user@server, or host=tcp://server:2376 plus TLS certificates) and an optional description. They live under ~/.docker/contexts/, and docker context ls shows them with an asterisk on the current one. Docker Desktop creates contexts like desktop-linux automatically.

How the CLI decides which daemon to use

The order of precedence, highest first:

  1. The --context <name> (or -c) flag on a single command.
  2. The DOCKER_HOST environment variable, which overrides any context.
  3. The DOCKER_CONTEXT environment variable.
  4. The current context set by docker context use.

This precedence is a common source of "why is my command hitting the wrong server" bugs. If DOCKER_HOST is exported in your shell profile, switching contexts appears to do nothing.

Real-world uses

  • Deploying to a VPS without a CI agent. Create an SSH context for the server and run docker compose up -d from your laptop; the Compose file is read locally, but containers start on the remote host.
  • Using a bigger build machine. Point a context at a powerful remote box and build there. The build context directory is uploaded to the remote daemon.
  • Keeping environments separate. Having dev, staging and prod contexts with clear names is safer than juggling IP addresses.

Gotchas

  • Bind mounts resolve on the remote host. -v ./config:/app/config mounts the server's ./config, not your laptop's. Use volumes or copy files into the image.
  • Never expose the daemon on plain TCP. Port 2375 without TLS gives anyone root on the host. Prefer SSH (ssh://), which reuses your keys and needs no open Docker port, or TCP on 2376 with mutual TLS.
  • SSH needs Docker on the remote PATH and the remote user in the docker group (or using rootless Docker).
  • Production safety. It is easy to forget you are on the prod context. Show the current context in your shell prompt, or use --context prod explicitly for prod commands.
  • Legacy cloud contexts are gone. The old docker context create ecs and aci integrations from the Compose CLI were retired in 2023; current contexts are Docker endpoints only.
One CLI, two daemons. The active context decides which Docker daemon receives each command.
  1. Docker CLI → Local daemon: docker ps (context: default)
  2. Local daemon → Docker CLI: Local container list
  3. Docker CLI → Remote server: docker context use staging
  4. Docker CLI → Remote server: docker compose up -d over SSH
  5. Remote server → Docker CLI: Containers started remotely

Code examples

Create and switch between contexts

# Remote host over SSH (uses your ~/.ssh keys)
docker context create staging \
  --description "Staging VM in ap-south-1" \
  --docker "host=ssh://deploy@203.0.113.10"

docker context ls

# Switch for all following commands
docker context use staging
docker compose up -d

# One-off command against another context
docker --context default ps

# Back to local
docker context use default

SSH contexts need no exposed Docker port. `--context` runs a single command elsewhere without changing the active context.

TLS context for a TCP endpoint

docker context create prod \
  --docker "host=tcp://prod.example.internal:2376,ca=$HOME/certs/ca.pem,cert=$HOME/certs/cert.pem,key=$HOME/certs/key.pem"

# Check what a context points to
docker context inspect prod --format '{{.Endpoints.docker.Host}}'

Port 2376 with mutual TLS is the secure TCP option; never expose an unauthenticated daemon on 2375.

Key points

  • A context is a named daemon endpoint plus credentials, stored in ~/.docker/contexts.
  • DOCKER_HOST overrides the active context; --context overrides both for one command.
  • SSH contexts are the simplest secure way to manage a remote host.
  • Bind mount paths and builds run on the remote daemon, not your laptop.
  • Old ECS/ACI cloud contexts were retired; contexts now point only at Docker endpoints.

Common follow-up questions

  • Why does switching context seem to have no effect on some machines?
  • How would you deploy a Compose app to a VPS using contexts?
  • What are the risks of exposing the Docker daemon over TCP?
  • Where does the build actually run when you use a remote context?

Interview tips

  • Mention the DOCKER_HOST precedence trap; it shows real debugging experience.
  • Recommend SSH over raw TCP and explain why an exposed daemon equals root access.
  • Point out the remote bind mount behaviour, a subtle detail most candidates miss.

Concepts covered

docker context, DOCKER_HOST, SSH Endpoint, Remote Daemon, TLS

More Docker & Containers questions