---
title: "Managing Local and Remote Daemons with Docker Contexts"
date: "2026-11-17T11:30:00+07:00"
description: "Learn how to use Docker contexts to seamlessly switch between local and remote Docker daemons over SSH and TLS without changing environment variables."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/docker-contexts-managing-local-remote-daemons.jpg"
alt: "Conceptual illustration of a single Docker CLI managing multiple local, staging, and remote cloud daemons via Docker Contexts."
tags: ['docker', 'containers', 'cli', 'devops', 'ssh', 'security']
---

Managing multiple Docker engines across local workstations, staging servers, and remote cloud virtual machines traditionally required manually toggling the `DOCKER_HOST` and `DOCKER_CERT_PATH` environment variables.

Introduced to streamline multi-engine management, **Docker Contexts** allow developers and operators to save, inspect, and instantly switch between multiple local and remote Docker daemons directly from a single CLI binary.

---

## 1. What is a Docker Context?

A **Docker Context** encapsulates all connection parameters required to interact with a target Docker daemon endpoint into a single named profile.

A context configuration stores:
- **Host Endpoint:** Unix socket (`unix:///var/run/docker.sock`), SSH connection (`ssh://user@host`), or TCP socket (`tcp://host:2376`).
- **TLS Security Certificates:** CA root certificates, client certificates, and private client keys for encrypted TCP endpoints.
- **Orchestrator Metadata:** Docker Swarm or Kubernetes cluster references.

```
                            +-----------------------------+
                            |     Local Docker CLI        |
                            +-----------------------------+
                                          |
                +-------------------------+-------------------------+
                |                         |                         |
  docker context use default   docker context use staging   docker context use prod
                |                         |                         |
                v                         v                         v
        [Local Unix Socket]        [Remote SSH Socket]      [Remote Secure TLS]
     unix:///var/run/docker.sock   ssh://deploy@10.0.1.20   tcp://prod.example.com:2376
```

---

## 2. Managing Contexts via CLI

### A. Listing Active Contexts

To view all configured contexts on your local system:

```bash
docker context ls
```

*Output:*
```text
NAME        DESCRIPTION                               DOCKER ENDPOINT               ERROR
default *   Current DOCKER_HOST (.../docker.sock)     unix:///var/run/docker.sock   
staging     Staging Environment Server                ssh://ubuntu@10.0.1.20       
prod        Production Enterprise Cluster             tcp://prod.example.com:2376   
```

The asterisk (`*`) indicates the currently active context target.

---

## 3. Creating Remote Contexts

### Method 1: SSH Remote Daemon (Recommended)

Connecting over **SSH** is the most secure method to manage remote Docker daemons because it leverages your existing SSH key infrastructure (`ssh-agent`) without exposing unencrypted or public TCP ports on the target server.

```bash
docker context create staging-server \
  --description "Staging Cloud VM" \
  --docker "host=ssh://deploy@staging.example.com:22"
```

*Prerequisites:* Ensure your public SSH key is present in the remote user's `~/.ssh/authorized_keys` file and that the user belongs to the `docker` group on the target host.

### Method 2: TLS Socket Connection

For environments using TLS mutual authentication over TCP:

```bash
docker context create prod-cluster \
  --description "Production TLS Daemon" \
  --docker "host=tcp://prod-node.internal:2376,ca=~/.docker/ca.pem,cert=~/.docker/cert.pem,key=~/.docker/key.pem"
```

---

## 4. Switching and Executing Across Contexts

### A. Switching the Active Context Globally

To switch your CLI target globally so that subsequent `docker` and `docker compose` commands execute on the remote daemon:

```bash
# Switch to staging server
docker context use staging-server

# Verify target daemon info
docker info

# Run a container on the STAGING server directly from your local terminal!
docker run -d --name nginx-staging -p 80:80 nginx:alpine

# Switch back to local desktop daemon
docker context use default
```

### B. One-Off Command Overrides (`--context` / `DOCKER_CONTEXT`)

You can target a specific context for a single command execution without changing your global active context:

```bash
# Execute docker ps on production without switching global context
docker --context prod-cluster ps

# Or set an inline environment variable override
DOCKER_CONTEXT=staging-server docker compose up -d
```

---

## 5. Docker Compose with Remote Contexts

Docker Contexts integrate natively with **Docker Compose**. When you target a remote SSH context, `docker compose up` automatically builds or pulls images and instantiates services directly on the remote node:

```bash
# Deploy a multi-container stack directly to a remote staging server
docker compose --context staging-server up -d --build
```

```
[Local Machine CLI] ---(SSH Tunnel)---> [Remote Staging Host]
- Reads local docker-compose.yml        - Pulls & builds layers
- Sends deployment API commands         - Starts container stack
```

---

## Key Takeaway

**Takeaway:** Docker Contexts replace error-prone environment variable manipulation with clean, named profiles (`docker context create/use`). Connecting over `ssh://user@host` provides zero-configuration, encrypted remote daemon management using existing SSH keys, allowing developers to execute CLI commands and `docker compose` deployments directly against remote servers from a single workstation.

---

## Sources

- [Docker Documentation: Manage Docker contexts](https://docs.docker.com/engine/context/working-with-contexts/)
- [Docker Documentation: Protect the Docker daemon socket (TLS / SSH)](https://docs.docker.com/engine/security/protect-access/)
- [Docker Documentation: Docker CLI `--context` flag reference](https://docs.docker.com/engine/reference/commandline/cli/#override-the-default-docker-context)
