How to use docker context inspect command to view context details

DockerDockerBeginner
Practice Now

Introduction

In this lab, we will explore how to use the docker context inspect command to view detailed information about Docker contexts. Docker contexts are essential for managing connections to different Docker daemons. We will begin by inspecting the default Docker context, which connects to the local daemon.

Following that, we will create a new context for demonstration purposes and then use docker context inspect to examine its details by name. Finally, we will learn how to format the output of the inspect command in different ways, specifically as JSON and using a Go template, to extract specific information about a context.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") subgraph Lab Skills docker/inspect -.-> lab-555132{{"How to use docker context inspect command to view context details"}} docker/create -.-> lab-555132{{"How to use docker context inspect command to view context details"}} end

Inspect the default Docker context

In this step, we will explore the default Docker context. A Docker context is a way to manage connections to different Docker daemons. By default, Docker uses a context named default which connects to the local Docker daemon.

To inspect the default Docker context, we use the docker context inspect command followed by the context name.

docker context inspect default

This command will output detailed information about the default context, including its endpoint, type, and other configuration details. The output will be in JSON format.

[
  {
    "Name": "default",
    "Current": true,
    "Endpoint": {
      "Docker": {
        "Host": "unix:///var/run/docker.sock"
      }
    },
    "Metadata": {},
    "Storage": "meta/contexts/default"
  }
]

The output shows that the default context is the current context ("Current": true) and its endpoint is set to unix:///var/run/docker.sock, which is the default Unix socket for the Docker daemon on Linux systems.

Create a new context for demonstration

In this step, we will create a new Docker context. Creating a new context allows you to define a connection to a different Docker daemon, which could be on a remote machine or a different configuration on the same machine. For this demonstration, we will create a context that still points to the local Docker daemon, but it will have a different name.

To create a new context, we use the docker context create command followed by the name of the new context and the endpoint information. Since we are connecting to the local daemon, the endpoint will be the same as the default context: unix:///var/run/docker.sock.

Let's create a new context named my-context.

docker context create my-context --docker "host=unix:///var/run/docker.sock"

After running this command, you should see output indicating that the context has been created.

my-context

This confirms that a new context named my-context has been successfully created, pointing to the local Docker daemon.

Inspect the newly created context by name

In the previous step, we created a new Docker context named my-context. Now, let's inspect this newly created context to see its details. Similar to inspecting the default context, we use the docker context inspect command, but this time we specify the name of our new context.

docker context inspect my-context

This command will display the configuration details for the my-context we just created. The output will again be in JSON format.

[
  {
    "Name": "my-context",
    "Current": false,
    "Endpoint": {
      "Docker": {
        "Host": "unix:///var/run/docker.sock"
      }
    },
    "Metadata": {},
    "Storage": "meta/contexts/my-context"
  }
]

Notice that the output for my-context is similar to the default context, as both point to the local Docker daemon. However, the "Current" field is false for my-context, indicating that it is not the currently active context. The default context is still the active one.

Inspect the context and format output as JSON

In this step, we will explicitly request the output of the docker context inspect command to be in JSON format. Although the default output is already JSON, using the --format json flag is a good practice when you specifically need JSON output, for example, when piping the output to other tools for processing.

We will inspect the my-context we created in the previous step and format the output as JSON.

docker context inspect my-context --format json

This command will produce the same JSON output as in the previous step, but it explicitly demonstrates how to use the --format flag to specify the output format.

[
  {
    "Name": "my-context",
    "Current": false,
    "Endpoint": {
      "Docker": {
        "Host": "unix:///var/run/docker.sock"
      }
    },
    "Metadata": {},
    "Storage": "meta/contexts/my-context"
  }
]

Using the --format json flag is particularly useful when you want to programmatically parse the output of Docker commands.

Inspect the context and format output using a Go template

In this final step, we will explore how to format the output of docker context inspect using a Go template. This is a powerful feature that allows you to extract specific information from the JSON output and display it in a custom format.

We will inspect the my-context and use a Go template to display only the context name and the Docker host endpoint. The template syntax uses double curly braces {{ }} to enclose expressions. We can access fields within the JSON output using dot notation, for example, .Name for the context name and .Endpoint.Docker.Host for the Docker host endpoint.

docker context inspect my-context --format '{{.Name}}: {{.Endpoint.Docker.Host}}'

This command will use the provided Go template to format the output. You should see the name of the context followed by its Docker host endpoint.

my-context: unix:///var/run/docker.sock

This demonstrates how Go templates can be used to customize the output of Docker commands, making it easier to extract and process specific data.

Summary

In this lab, we learned how to use the docker context inspect command to view details about Docker contexts. We started by inspecting the default context, which connects to the local Docker daemon. We then created a new context named my-context for demonstration purposes, also pointing to the local daemon.

We further explored the docker context inspect command by inspecting the newly created context by name. Finally, we demonstrated how to format the output of the inspect command in different ways, specifically as JSON and using a Go template, to extract and display specific information about a context.