Control remote containers with Docker contexts over SSH

Local command, remote daemon

Geschrieben von Timo Rieber am 12. April 2025

I got tired of SSH-ing into servers just to run docker ps or restart a container. Docker contexts let you talk to a remote Docker daemon straight from your local terminal.

Set up a context

Docker's SSH transport requires public key authentication. Password login is not supported. Set up SSH keys if you haven't already. You need Docker on both machines.

Create a context that points to your remote daemon:

docker context create <context-name> \
  --docker "host=ssh://<user>@<your.remote.host>"
Bash

Activate it until you switch again:

docker context use <context-name>
Bash

Or pass it per command with docker --context .

Run commands on the remote host

From here on, every Docker command targets the remote daemon. List running containers:

docker ps
Bash

Start a test container on the remote host:

docker run \
  --rm --detach \
  --name nginx-test \
  --publish 8080:80 \
  nginx
Bash

The container is now running on the remote server, accessible via http://:8080.

Every Docker command works the same as locally, including interactive shells:

docker exec --interactive --tty nginx-test bash
Bash

Exit the shell with CTRL+D and stop the container. The --rm flag removes it automatically:

docker stop nginx-test
Bash

Switch back to local with docker context use default.

In practice, I use this to manage cloudapps, texsite, and mainzelmen across staging and production servers, checking container health, debugging issues, and kicking off deployments, all without an SSH session.