How to use docker service inspect command to view service details

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker service inspect command to view detailed information about Docker services. You will explore different ways to inspect a service, including by its name and by its unique ID.

Furthermore, you will discover how to format the output of the docker service inspect command using the --pretty and --format options to control the level of detail and the presentation of the service information.


Skills Graph

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

Inspect a service by name

In this step, you will learn how to inspect a Docker service by its name. The docker service inspect command provides detailed information about a service, including its configuration, tasks, and network settings.

First, let's create a simple service to inspect. We will create a service named my-web-service using the nginx image. Since Docker Compose is not pre-installed, we will use the docker service create command directly.

docker service create --name my-web-service nginx

You should see output indicating that the service was created. Docker will pull the nginx image if it's not already present on your system.

Now that the service is created, you can inspect it by name using the docker service inspect command followed by the service name.

docker service inspect my-web-service

This command will output a large JSON object containing all the details about the my-web-service. This output can be quite extensive, providing a deep look into the service's configuration and state.

Inspect a service by ID

In the previous step, you learned how to inspect a service by its name. Docker also assigns a unique ID to each service. You can use this ID to inspect the service as well. This is particularly useful when you have multiple services with similar names or when scripting.

First, let's get the ID of the my-web-service we created in the previous step. You can use the docker service ls command to list all running services and their IDs.

docker service ls

The output will show a table with columns like ID, NAME, MODE, REPLICAS, and IMAGE. The ID column contains the unique identifier for each service. Copy the ID for the my-web-service. It will be a string of characters.

Now, use the docker service inspect command followed by the service ID you copied. Replace <SERVICE_ID> with the actual ID.

docker service inspect <SERVICE_ID>

You will see the same detailed JSON output as when you inspected by name. This demonstrates that you can use either the service name or its ID to get the inspection details. Using the ID is often more precise, especially in automated scripts.

Format output using --pretty

In the previous steps, you saw that the default output of docker service inspect is a large JSON object. While comprehensive, this format can be difficult to read for a quick overview. Docker provides the --pretty flag to output the information in a more human-readable format.

Let's inspect the my-web-service again, this time using the --pretty flag.

docker service inspect --pretty my-web-service

You will notice that the output is now formatted in a way that is easier to scan and understand. It presents key information like the service ID, name, mode, replicas, and image in a structured layout, rather than a raw JSON dump. This is very useful for quickly checking the status and basic configuration of a service.

The --pretty flag is a convenient way to get a summary of the service details without having to parse the full JSON output.

Format output using --format

While --pretty provides a human-readable summary, the --format flag gives you fine-grained control over the output format using Go template syntax. This is incredibly powerful for extracting specific pieces of information or formatting the output for scripting or reporting.

The Go template syntax allows you to access fields within the JSON output of the inspect command. For example, to get just the service ID and name, you can use a format string like {{.ID}} {{.Spec.Name}}.

Let's try inspecting the my-web-service and formatting the output to show only the service ID and name.

docker service inspect --format '{{.ID}} {{.Spec.Name}}' my-web-service

You should see output similar to:

<SERVICE_ID> my-web-service

where <SERVICE_ID> is the actual ID of your service.

You can access nested fields using dots. For instance, to get the image name, you would use {{.Spec.TaskTemplate.ContainerSpec.Image}}.

Let's try another example to get the service name and the image it's using.

docker service inspect --format 'Service Name: {{.Spec.Name}}, Image: {{.Spec.TaskTemplate.ContainerSpec.Image}}' my-web-service

The output will be:

Service Name: my-web-service, Image: nginx

The --format flag is very flexible and allows you to tailor the output to your specific needs. You can explore the full JSON output (without --pretty or --format) to understand the structure and identify the fields you want to extract.

Summary

In this lab, you learned how to use the docker service inspect command to view detailed information about Docker services. You practiced inspecting a service by its name, first by creating a simple nginx service named my-web-service using docker service create, and then using docker service inspect my-web-service to retrieve its configuration and state in JSON format.

You also learned how to inspect a service using its unique ID. By using docker service ls to find the service ID, you were able to use docker service inspect <SERVICE_ID> to obtain the same detailed information, demonstrating an alternative method for inspecting services, particularly useful in scripting or when names are ambiguous.