How to Use Docker Context to Manage Multiple Remote Docker Hosts over SSH

Background & Why Use Docker Context?

Suppose you manage three servers: a staging VPS (2GB RAM), a production VPS (8GB RAM), and a dedicated database cluster. Your typical daily workflow probably looks like this:

ssh [email protected]
# Wait for the connection, then run docker ps, docker logs...
exit
ssh [email protected]
# Run docker compose up -d again...

Every time you need to check logs or debug an issue, you end up opening 3–4 terminal tabs. This workflow is not only slow but also prone to costly mistakes, like accidentally running a staging command on production.

The traditional workaround was opening port 2375 (unencrypted TCP socket) or port 2376 (TLS) and pointing the DOCKER_HOST environment variable. However, exposing port 2375 is practically handing root access on your VPS to any scanning botnet. While configuring TLS is more secure, it is tedious: you have to generate a custom CA, distribute certificates, and maintain them before they expire.

Docker Context solves this problem much more elegantly. It allows you to store a list of endpoints (local sockets, SSH tunnels) right on your local machine. Once configured, switching target environments takes a single command. All subsequent docker and docker compose commands will execute directly on the remote VPS over an existing secure SSH connection.

Instead of deploying Portainer and consuming an extra 100–200MB of RAM on small VPS instances, Docker Context leverages your server’s default SSH daemon. This makes managing infrastructure significantly lighter.

Setting Up Passwordless SSH Authentication

Docker Context directly utilizes your operating system’s native SSH client. You only need to configure SSH keys once so that the Docker client can connect seamlessly without prompting for a password.

Step 1: Generate an SSH Key on Your Local Machine

Generate a new key pair using the ed25519 algorithm for high security and fast handshakes:

ssh-keygen -t ed25519 -C "docker-remote-context"

Press Enter to save the key to the default path ~/.ssh/id_ed25519.

Step 2: Copy the Public Key to Remote VPS Servers

Copy your public key to each server with a single command:

ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]
ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]

Step 3: Grant Docker Permissions Without sudo on the Remote Host

By default, the Docker daemon requires root privileges. Add your user to the docker group to prevent permission denied errors:

# Run on each target VPS
sudo usermod -aG docker $USER
# Apply the new permissions immediately without logging out:
newgrp docker
docker ps

Step 4: Configure Your Local SSH Config File

Setting aliases for each server in ~/.ssh/config makes commands much shorter and cleaner:

Host staging-vps
    HostName 192.0.2.10
    User deployer
    IdentityFile ~/.ssh/id_ed25519
    Port 22

Host prod-vps
    HostName 198.51.100.20
    User deployer
    IdentityFile ~/.ssh/id_ed25519
    Port 22

Verify your setup with: ssh staging-vps. If you connect directly to the VPS without being prompted for a password, your preparation is complete.

Configuring and Using Docker Context

Now you are ready to create and switch between environments directly from your computer.

Checking Existing Contexts

Run this command to view all contexts available on your system:

docker context ls

The column marked with an asterisk * indicates the currently active context. By default, it will be default (pointing to your local Docker socket).

Creating New Contexts Pointing to Remote VPS Hosts

The syntax for creating a context over SSH is very straightforward:

# Create context for staging
docker context create vps-staging \
  --description "VPS Staging 2GB" \
  --docker "host=ssh://staging-vps"

# Create context for production
docker context create vps-prod \
  --description "VPS Production 8GB" \
  --docker "host=ssh://prod-vps"

If you prefer not to use an SSH alias, pass the full user and IP address: host=ssh://[email protected]:22.

Switching the Working Context

Switch your entire working session to the staging VPS:

docker context use vps-staging

Spin up a web server to test the connection:

docker ps
docker run -d -p 8080:80 --name test-nginx nginx:alpine

The test-nginx container is now running in the background on the staging VPS (IP 192.0.2.10). Your local machine consumes virtually no resources.

Running One-Off Commands with the –context Flag

You don’t need to change the global context for a quick one-off operation. Simply pass the --context flag directly:

docker --context vps-prod ps
docker compose --context vps-staging up -d

This approach is especially safe when writing CI/CD scripts or Makefiles, as it prevents accidental deployments caused by forgetting to switch contexts.

Monitoring and Inspecting Remote VPS Hosts

All your familiar local debugging workflows can now be applied directly to remote servers.

1. Verifying the Connected Host

Avoid costly mistakes by inspecting the target host information before running major operations:

docker info | grep -E "Name|Operating System|Total Memory|Server Version"

The output will display the hostname, operating system (e.g., Ubuntu/Debian), and actual RAM capacity of the remote VPS.

2. Monitoring Container Resources in Real Time

Check CPU and memory consumption across all containers on the VPS:

docker stats --no-stream
# Or tail the last 50 log lines without logging into the server:
docker logs -f --tail 50 test-nginx

3. Cleaning Up Disk Space Remotely

Inspect disk space consumed by build cache, dangling images, and unused volumes:

docker system df

If you spot tens of gigabytes of lingering build cache, clean it up directly from your local terminal:

docker system prune -af --volumes

4. Switching Back to the Local Environment

Once you are done with the remote VPS, simply run:

docker context use default

With Docker Context and SSH, you keep your Docker sockets hidden from the public Internet while managing multi-server infrastructure centrally and effortlessly right from your local terminal.

Share: