How to use docker context ls command to list contexts

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker context ls command to manage and view your Docker contexts. Docker contexts are essential for easily switching between different Docker daemons, whether they are local, remote, or cloud-based.

Through hands-on steps, you will explore how to list all available contexts with detailed information, filter the output to display only context names, and format the output in various ways, including JSON and using custom templates. This will equip you with the skills to quickly identify and understand your configured Docker environments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") subgraph Lab Skills docker/ls -.-> lab-555133{{"How to use docker context ls command to list contexts"}} end

List all Docker contexts

In this step, you will learn how to list all available Docker contexts using the docker context ls command. Docker contexts allow you to switch between different Docker daemons easily, whether they are running locally, on a remote machine, or in a cloud environment.

To list all Docker contexts, open your terminal and run the following command:

docker context ls

This command will display a table showing information about each Docker context, including its name, description, endpoint, and whether it is the current context.

You should see output similar to this:

NAME                DESCRIPTION                               DOCKER ENDPOINT                                 KUBERNETES ENDPOINT   ORCHESTRATOR
default             Current DOCKER_HOST environment             unix:///var/run/docker.sock                     n/a                   swarm

The default context is the one that is automatically created when you install Docker and points to the local Docker daemon.

List only Docker context names

In the previous step, you listed all Docker contexts with detailed information. Sometimes, you might only need to see the names of the contexts. You can achieve this by using the --format flag with the docker context ls command.

The --format flag allows you to specify the output format using Go's text/template package. To list only the names, you can use the template {{.Name}}.

Run the following command in your terminal:

docker context ls --format "{{.Name}}"

This command will output only the names of the Docker contexts, one name per line.

You should see output similar to this:

default

This is useful when you want to quickly see the available context names, perhaps for scripting or further processing.

Format Docker context list output as JSON

In addition to listing names or the default table format, you can also format the output of docker context ls as JSON. This is particularly useful when you need to process the context information programmatically.

To format the output as JSON, use the --format flag with the value json.

Run the following command in your terminal:

docker context ls --format json

This command will output a JSON array, where each element in the array represents a Docker context.

You should see output similar to this:

[
  {
    "Name": "default",
    "Current": true,
    "Endpoint": "unix:///var/run/docker.sock",
    "Description": "Current DOCKER_HOST environment",
    "DockerEndpoint": "unix:///var/run/docker.sock",
    "KubernetesEndpoint": "",
    "Orchestrator": "swarm"
  }
]

The JSON output provides a structured way to access all the details of each Docker context.

Format Docker context list output using a custom template

In the previous steps, you saw how to list all contexts, just their names, and format the output as JSON. The --format flag is very powerful and allows you to define custom output templates using Go's text/template syntax.

You can specify which fields to display and how to format them. For example, let's list the context name and its description in a custom format. The available fields you can use in the template are Name, Description, DockerEndpoint, KubernetesEndpoint, and Orchestrator.

Run the following command in your terminal:

docker context ls --format "Context Name: {{.Name}}, Description: {{.Description}}"

This command uses a custom template to display the name and description of each context.

You should see output similar to this:

Context Name: default, Description: Current DOCKER_HOST environment

You can create more complex templates to include other fields or format the output differently based on your needs. This flexibility is very useful for scripting and integrating Docker context information into other tools.

Summary

In this lab, you learned how to use the docker context ls command to list Docker contexts. You started by listing all available contexts with detailed information, including their name, description, endpoint, and orchestrator. You then explored how to list only the names of the contexts using the --format "{{.Name}}" flag, which is useful for quick reference or scripting. Finally, you learned how to format the output as JSON using the --format json flag, providing a structured output suitable for programmatic consumption.