Docker contexts and remote engines
Drive Docker engines on other machines from your own CLI: contexts over SSH or TLS, how the CLI picks an endpoint, and what changes when the engine is remote.
The Docker architecture: client, daemon and runtimesThe Docker socket is root
Docker contexts and remote engines
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The docker CLI is only a client. A context is a named endpoint, plus any TLS material, that tells it which engine to talk to.
- `docker context create prod --docker host=ssh://deploy@prod.example.com` then `docker context use prod` points every later command, including `docker compose`, at that server.
- Over SSH, the CLI runs your system's `ssh` and starts `docker system dial-stdio` on the remote host, so your SSH config, keys and agent apply, and the remote user needs access to the engine there.
- The CLI picks an endpoint in this order: `--context`, then `-H/--host`, then DOCKER_HOST, then DOCKER_CONTEXT, then the current context in ~/.docker/config.json, then default.
- A context switch moves everything that runs, but not your files. Bind-mount paths refer to the remote host, and `docker build` uploads the build context from your machine.
- Prefer SSH. A TCP endpoint needs TLS with client certificates on port 2376; an unauthenticated 2375 listener is root for anyone who can reach it.
- Contexts live under ~/.docker/contexts/, in directories named by the SHA-256 of the context name, and can be moved between machines with `docker context export` and `import`.
Many engines, one CLI
You have Docker Desktop on your laptop, a staging VM and a production server. To check a container on staging, you SSH in and run docker ps there. To deploy, you copy a Compose file over and run it there. Before long you are juggling terminal tabs and DOCKER_HOST exports, and one day you run docker compose down -v in the wrong one.
The architecture lesson showed that the docker command is only a client sending API requests to an engine. Nothing requires that engine to be on the same machine. A context is a saved answer to “which engine?”, like a saved connection in a database client.
Creating and using contexts
docker context create prod \
--description "production server" \
--docker "host=ssh://deploy@prod.example.com"
docker context ls
docker context use prod # every later command goes to prod
docker ps # prod's containers
docker context use default # back to the local engine
For one command, don’t switch at all: docker --context prod logs api. For a script or a single Compose run, DOCKER_CONTEXT=prod docker compose up -d. Compose uses the same endpoint as the CLI, which is what makes “deploy from my laptop” or “deploy from CI” possible without copying files to the server.
Docker Desktop creates contexts of its own, such as desktop-linux, which is how its CLI reaches the engine inside its VM.
How SSH contexts work
I read the client side in docker/cli’s source (cli/connhelper/connhelper.go). For an ssh:// host, the CLI runs your system’s ssh binary with the remote command docker system dial-stdio. That remote command connects to the engine’s socket on the server and relays the API traffic over the SSH connection’s standard input and output. If the URL has a path, such as ssh://deploy@prod/run/user/1000/docker.sock, the remote command gets --host=unix:// plus that path, which is how you reach a rootless daemon.
Three consequences follow:
- Your normal SSH setup applies:
~/.ssh/confighost aliases, keys, the agent, jump hosts. Ifssh deploy@prod.example.comworks, the context works. - The server needs the
dockerCLI installed, because it runsdocker system dial-stdio. - The remote user must be allowed to use the engine, usually through the
dockergroup, which is root on that server. Give that SSH account the care you’d give a root login.
Every command opens an SSH connection, which adds latency. SSH connection sharing (ControlMaster auto and ControlPersist in ~/.ssh/config) makes repeated commands much faster.
TCP with TLS
The alternative is a TCP endpoint with mutual TLS: the daemon listens on 2376 with --tlsverify, and each client has a certificate signed by your CA.
docker context create prod-tls --docker \
"host=tcp://prod.example.com:2376,ca=$HOME/certs/ca.pem,cert=$HOME/certs/cert.pem,key=$HOME/certs/key.pem"
It works, but you are now running a certificate authority. SSH reuses access control you already have. What you must never do is expose 2375 without TLS: anyone who can reach it has root on the server.
Which endpoint wins
Several settings can name an engine, and the CLI resolves them in a fixed order. From resolveContextName in docker/cli’s cli/command/cli.go:
- the
--contextflag - the
-H/--hostflag (uses the default context with that host) - the
DOCKER_HOSTenvironment variable - the
DOCKER_CONTEXTenvironment variable currentContextin~/.docker/config.json, set bydocker context usedefault
Item 3 above item 5 is the classic trap: a DOCKER_HOST left in your shell profile silently overrides docker context use. The CLI prints a warning when you switch contexts with DOCKER_HOST set. docker context show prints the context actually in effect.
Where contexts are stored
The store is plain files under ~/.docker/contexts/. Its package documentation describes meta/<id>/meta.json for the endpoint and tls/<id>/ for certificates, where the ID is the SHA-256 of the context name. I computed it for a context called prod:
$ printf 'prod' | shasum -a 256
6754af9632a2745e85c293e5aac0863370d9bd3330b9938c00cadfd215227d77
so its metadata would be at ~/.docker/contexts/meta/6754af96.../meta.json. Hashing avoids problems with names that aren’t valid file names. To give a colleague or a CI runner the same context, docker context export prod writes a tar file, and docker context import reads it. Remember that an export of a TLS context contains its private key.
What changes when the engine is remote
A context moves where things run. It doesn’t move your files.
- Bind mounts name paths on the engine’s host.
-v ./data:/datawith theprodcontext mounts./dataon the server, which is probably empty or missing. Use named volumes on remote engines. - Builds upload the build context from your machine to the remote engine, then build there. On a slow link, a large context hurts, so a good
.dockerignorematters even more. - Published ports open on the remote host, not on
localhost. - Images are pulled by the remote engine from the registry, not copied from your laptop. Push first, or build on the remote engine.
About the video
The embed is all of “Some Context on Docker Contexts”, a talk by Jacob Howard from Docker’s December 2021 Community All-Hands, on Docker’s channel, about 11 minutes. The chapters cover the Docker architecture, the challenges of connecting to other engines, use cases for remote engines, what contexts are, and a practical demonstration. That is this lesson’s path, from someone who worked on Docker. It predates current Docker Desktop’s context names, and I judged it from its chapters and description, not a transcript. YouTube reports no audio language, so English was judged from the title, description and English auto-captions. Elton Stoneman’s video in deeper sets up remote access with both TLS and SSH.
What was run: the SSH connection helper, the context resolution order and the context store layout were read from docker/cli’s source, and the SHA-256 of a context name was computed with shasum. No context was created and no remote engine contacted, since no Docker CLI or daemon was available. Commands are from Docker’s context and daemon-protection documentation, checked 2026-11-28.
Where this fits
That completes the Docker roadmap. From here, running containers across many machines is the job of an orchestrator: DevOps’ why orchestration exists is the next step.
Check yourself
You run docker context use prod, but docker ps still shows your laptop's containers. What is the likely cause?
DOCKER_HOST is set in your shell. It takes precedence over the current context in the config file, and the CLI warns about this when you switch.
With the prod context active, docker run -v ./data:/data app fails or mounts an empty directory. Why?
The engine runs on the prod server, so the bind-mount path is resolved on that server's filesystem, not your laptop's. Use a named volume, or copy the data to the server first.
What does the remote server need for an ssh:// context to work?
An SSH login for the user, the docker CLI installed (the client runs docker system dial-stdio there), and permission for that user to reach the engine's socket, such as docker group membership or a rootless daemon.
How do you run one command against staging without changing the current context?
docker --context staging ps, or DOCKER_CONTEXT=staging docker compose up -d for a single invocation.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.