Overview
When I first used Docker, I wanted to do something on the server and had to run the SSH command to access the server. I noticed that a lot of devs are just like me! Although I have learned and know how to use Docker to build a development environment, I accidentally missed an extremely useful feature. That is docker context
.
Case studies
Let’s first take a look at some common docker context usage examples.
1. Remote Docker Context over SSH
I will create a context named myvps as follows:
1 2 | docker context create myvps --docker <span class="token string">"host=ssh://nguyen.huu.kim@myvps"</span> |
Then, use the above context to run Docker CLI commands locally without having to access the server:
1 2 3 4 5 | docker context use myvps myvps Current context is now <span class="token string">"myvps"</span> |
2. Remote Docker Context with Kubernetes
For example, I will create a Context to connect Docker CLI to Docker host K8s Node running Docker. Here I use Minikube as an example.
1 2 | docker context create minikube --default-stack-orchestrator <span class="token operator">=</span> kubernetes --kubernetes config-file <span class="token operator">=</span> /home/nguyen.huu.kim/.kube/config --docker <span class="token assign-left variable">host</span> <span class="token operator">=</span> tcp://172.17.0.2:2376 |
Using the context just created above will help me run the Docker CLI command on the local machine without having to SSH into the server anymore. So what is Docker Context? Let’s find out with us!
What is Docker Context?
Image 1: Docker Architecture – Source: Docker
We manipulate Docker by running commands on the terminal through a set of commands called Docker CLI. The Docker CLI converts the command back into the corresponding API calls to Docker daemon
. All the descriptive information about how to connect to the API (or docker host) is gathered in a component called the Docker Context.
Information is stored
The information held by a Docker Context includes:
- Name : Name of the context
- Description : Description of the context
- Docker Endpoint : URL to connect to Docker daemon (including TLS information)
- Kubernetes Endpoint : URL to connect to Kubernetes cluster (in case of using k8s)
- Orchestrator : The type of architecture running on Docker, there are 2 options:
swarm
andkubernetes
1 2 3 | NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR default Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm |
By default, after installing Docker on Linux. A context configuration named default
will be set up. Docker CLI will read information from context to get connection information.
If other contexts are not added, then the Docker CLI will only interact with the Docker daemon on the same host. That is, any Docker machine can only be accessed by that machine according to the default
context.
Commonly used commands
List existing contexts
- Used to view all the contexts that have been saved on the device
- Parameters are rarely used, just remember
docker context ls
Command line
1 2 3 4 5 6 7 8 9 10 11 | Usage: docker context <span class="token function">ls</span> <span class="token punctuation">[</span> OPTIONS <span class="token punctuation">]</span> List contexts Aliases: ls, list Options: --format string Pretty-print contexts using a Go template -q, --quiet Only show context names |
For example
1 2 3 | NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR default Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm |
1. Add new context
- Used to create a new context on the machine
Command line
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | docker context create [OPTIONS] CONTEXT Create a context Docker endpoint config: NAME DESCRIPTION from Copy named context's Docker endpoint configuration host Docker endpoint on which to connect ca Trust certs signed only by this CA cert Path to TLS certificate file key Path to TLS key file skip-tls-verify Skip TLS certificate validation Kubernetes endpoint config: NAME DESCRIPTION from Copy named context's Kubernetes endpoint configuration config-file Path to a Kubernetes config file context-override Overrides the context set in the kubernetes config file namespace-override Overrides the namespace set in the kubernetes config file Example: $ docker context create my-context --description "some description" --docker "host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file" Options: --default-stack-orchestrator string Default orchestrator for stack operations to use with this context (swarm|kubernetes|all) --description string Description of the context --docker stringToString set the docker endpoint (default []) --from string create context from a named context --kubernetes stringToString set the kubernetes endpoint (default []) |
In which there are 2 different endpoint configuration sections depending on the orchestrator we are configuring:
- Docker endpoint config – Information needed when configuring according to Swarm
- Kubernetes endpoint config – Information needed when configuring under Kubernetes
For example
I will detail how to create context with each type in the next section (below).
2. Edit a context
- Used to edit and update information for an existing context on the machine
- The parameters are identical to the
docker context create
command
Command line
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | Usage: docker context update <span class="token punctuation">[</span> OPTIONS <span class="token punctuation">]</span> CONTEXT Update a context Docker endpoint config: NAME DESCRIPTION from Copy named context <span class="token string">'s Docker endpoint configuration host Docker endpoint on which to connect ca Trust certs signed only by this CA cert Path to TLS certificate file key Path to TLS key file skip-tls-verify Skip TLS certificate validation Kubernetes endpoint config: NAME DESCRIPTION from Copy named context'</span> s Kubernetes endpoint configuration config-file Path to a Kubernetes config <span class="token function">file</span> context-override Overrides the context <span class="token builtin class-name">set</span> <span class="token keyword">in</span> the kubernetes config <span class="token function">file</span> namespace-override Overrides the namespace <span class="token builtin class-name">set</span> <span class="token keyword">in</span> the kubernetes config <span class="token function">file</span> Example: $ docker context update my-context --description <span class="token string">"some description"</span> --docker <span class="token string">"host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file"</span> Options: --default-stack-orchestrator string Default orchestrator <span class="token keyword">for</span> stack operations to use with this context <span class="token punctuation">(</span> swarm <span class="token operator">|</span> kubernetes <span class="token operator">|</span> all <span class="token punctuation">)</span> --description string Description of the context --docker stringToString <span class="token builtin class-name">set</span> the docker endpoint <span class="token punctuation">(</span> default <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token punctuation">)</span> --kubernetes stringToString <span class="token builtin class-name">set</span> the kubernetes endpoint <span class="token punctuation">(</span> default <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token punctuation">)</span> |
For example
Update description
for the context named minikube
:
1 2 | docker context update minikube --description <span class="token string">"Docker Host for Minikube"</span> |
Output:
1 2 3 | minikube Successfully updated context <span class="token string">"minikube"</span> |
3. Delete a context
- Used to delete an existing context on the machine
- The accompanying parameters are often less needed, just ask
docker context rm
Command line
1 2 3 4 5 6 7 8 9 10 | Usage: docker context <span class="token function">rm</span> CONTEXT <span class="token punctuation">[</span> CONTEXT <span class="token punctuation">..</span> . <span class="token punctuation">]</span> Remove one or <span class="token function">more</span> contexts Aliases: rm, remove Options: -f, --force Force the removal of a context <span class="token keyword">in</span> use |
For example
1 2 | docker context <span class="token function">rm</span> minikube |
Output:
1 2 3 | minikube Successfully deleted context <span class="token string">"minikube"</span> |
4. Switch context
- Used to activate context from inactive -> active (not in use -> in use)
- Only one context is used at a time
Command line
1 2 3 4 | Usage: docker context use CONTEXT Set the current docker context |
For example
1 2 | docker context use minikube |
Output:
1 2 3 | minikube Current context is now <span class="token string">"minikube"</span> |
5. Others
There are also some other commands but rarely used, you can study more:
- docker context import
- docker context export
- docker context inspect