Remote Docker over SSH is set up with docker context create --docker "host=ssh://user@server" or by exporting DOCKER_HOST=ssh://user@server. The Docker client opens an SSH connection, usually on port 22, and talks to the remote daemon's socket. You never open port 2375 or manage TLS certificates for the daemon.
How remote Docker over SSH works without opening port 2375
With an ssh:// URL, the docker client does not connect to a daemon TCP port. It runs ssh against the server, executes docker system dial-stdio there and tunnels the API through that session. The daemon keeps listening only on /var/run/docker.sock. Authentication, encryption and access logging all come from OpenSSH.
The classic alternative is starting the daemon with -H tcp://0.0.0.0:2375, which exposes the API with no encryption and no authentication. Anyone who reaches that port can start a container with the host filesystem mounted, which amounts to root on the machine. With SSH, the only exposed port is 22, which you already had open to administer the server.
Creating a Docker context with docker context create and ssh://
A context stores the endpoint under a name, so you switch servers without touching environment variables:
docker context create prod-web --description "Web VPS over SSH" --docker "host=ssh://[email protected]"
docker context ls
docker context use prod-web
docker psIf sshd listens on a different port, put it in the URL: host=ssh://[email protected]:2222. To run a single command against a context without switching the active one, use the global --context option:
docker --context prod-web compose ps
docker context inspect prod-web
docker context use defaultThe default context points back to the local daemon. docker context ls marks the active context with an asterisk, which saves you from running docker rm on the wrong machine.
DOCKER_HOST=ssh:// for one-off remote Docker commands
DOCKER_HOST accepts the same ssh:// URL and suits a one-off command or a CI script:
DOCKER_HOST=ssh://[email protected] docker info
export DOCKER_HOST=ssh://[email protected]
docker images
unset DOCKER_HOSTDOCKER_HOST takes precedence over the context selected with docker context use. If the variable stays exported in your ~/.bashrc, switching contexts does nothing. Rule of thumb: contexts for interactive work, DOCKER_HOST only in scripts and pipelines.
5 server requirements for remote Docker over SSH
- Reachable OpenSSH. It listens on TCP port 22 by default; if you use another port, set it in the URL or in
~/.ssh/config. - Non-interactive key auth. Docker handles password or passphrase prompts poorly; load the key into
ssh-agent. OpenSSH has supported Ed25519 since version 6.5. - Correct permissions.
~/.sshat 700;authorized_keysand the private key at 600. Looser permissions get the key rejected. - Socket access. The remote user must be in the docker group (
sudo usermod -aG docker deploy). That group is effectively root. - docker CLI on the server. The connection runs
docker system dial-stdioremotely, so thedockerbinary must be on that user's PATH.
Speeding up docker context over SSH with ControlMaster in ~/.ssh/config
Every docker command opens a fresh SSH connection; OpenSSH multiplexing reuses one and removes the handshake delay:
Host prod-web
HostName 203.0.113.10
User deploy
IdentityFile ~/.ssh/id_ed25519
ControlMaster auto
ControlPath ~/.ssh/cm-%r@%h:%p
ControlPersist 10mWith that block, create the context with --docker "host=ssh://prod-web" and it inherits user, key and port. Every directive is documented in the official ssh_config manual page.
Remote Docker over SSH from a phone versus a desktop
iOS and Android have no native docker client: the phone opens SSH to a machine that has one (the server itself, a bastion or your Mac) and the contexts live there.
| What is measured | iOS/Android with an SSH client | Linux/macOS desktop |
|---|---|---|
| Where the docker client runs | On the server or bastion you connect to | On the local machine |
| Using docker context | Contexts defined on the bastion, used over SSH | Local contexts pointing at each server |
Ctrl+C to stop docker logs -f | Needs a keyboard with a Ctrl key | Physical keyboard |
| Switching from wifi to mobile data | Mosh keeps the session; tmux keeps processes | Rare on a fixed connection |
| Copying files to the server | SFTP inside the app | scp, which uses SFTP since OpenSSH 9.0 |
In the termi1 SSH and Mosh terminal, the custom keyboard adds Ctrl, Esc, Tab and arrows to leave docker logs -f or walk your history. If you leave a docker compose pull running inside tmux and the network changes, Mosh keeps the session alive. The AI turns “remove unused images” into docker image prune -a and types it at the prompt without pressing Enter; on connections tagged as production, Production Guard holds that command back before it reaches the prompt. Keys generated on the phone stay in the system secure store and never leave the device. To use your Mac as the bastion, termi1 Companion (macOS 14 or later) pairs it with a six-digit code.
4 mistakes when using docker context with ssh://
- A forgotten DOCKER_HOST. It overrides the active context; run
unset DOCKER_HOSTand check again withdocker context ls. - Passphrase key without an agent. The connection fails or hangs; add the key with
ssh-addbefore using the context. - User outside the docker group. You get permission denied on
/var/run/docker.sock; add the group and open a new session. - Old RSA key. Since OpenSSH 8.8,
ssh-rsasignatures with SHA-1 are disabled by default; generate an Ed25519 key.
Frequently asked questions
Do I need to open port 2375 for remote Docker?
Remote Docker does not need port 2375 when you use an ssh:// URL in a context or in DOCKER_HOST. The client logs in over SSH, usually on port 22, and talks to the daemon's local socket, which is never exposed on the network.
What is the difference between docker context and DOCKER_HOST?
A docker context stores a named endpoint selected with docker context use; DOCKER_HOST is an environment variable that takes precedence over the active context. Contexts fit interactive work, while the variable fits scripts and CI pipelines that target a single host.
Can I use docker context from an iPhone or Android phone?
Docker context does not run on iOS or Android because neither has a native docker client. From the phone you open SSH to a bastion, a Mac or the server itself, and run contexts or plain docker commands there.
How do I reach a Docker server with no public IP over SSH?
A Docker server with no public IP can be reached over SSH through a private mesh network, without opening router ports. Options such as the Prialo private mesh network or Tailscale give the machine an address reachable from your own devices.