Docker Context: Extremely useful feature but 90% of brothers are often overlooked

Tram Ho

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:

Then, use the above context to run Docker CLI commands locally without having to access the server:

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.

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.png

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 and kubernetes

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

For example

1. Add new context

  • Used to create a new context on the machine

Command line

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

For example

Update description for the context named minikube :

Output:

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

For example

Output:

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

For example

Output:

5. Others

There are also some other commands but rarely used, you can study more:

  • docker context import
  • docker context export
  • docker context inspect
Share the news now

Source : Viblo