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.