TExcelTAD
Home › Docker Swarm › Inspecting Services

Inspecting Services

When something is off ("why is it 2/3?", "which image is actually running?", "did the update finish?"), the answer is in the swarm's state. This lesson tours the four commands you will use most: docker service inspect, docker service ps, docker service logs and docker node ps, plus Go-template --format queries for scripts.

docker service inspect

--pretty gives a readable summary of the service spec (the desired state) plus status information:

bash
$ docker service inspect --pretty shop_api

ID:             x1y2z3a4b5c6d7e8f9g0h1i2j
Name:           shop_api
Labels:
 com.docker.stack.image=ghcr.io/acme/shop-api:2.5.0
 com.docker.stack.namespace=shop
Service Mode:   Replicated
 Replicas:      3
UpdateStatus:
 State:         completed
 Started:       12 minutes ago
 Completed:     10 minutes ago
 Message:       update completed
Placement:
 Constraints:   [node.role==worker]
UpdateConfig:
 Parallelism:   1
 Delay:         10s
 On failure:    rollback
 Monitoring Period: 30s
 Max failure ratio: 0
 Update order:      start-first
RollbackConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Rollback order:    stop-first
ContainerSpec:
 Image:         ghcr.io/acme/shop-api:2.5.0@sha256:4b9d3c...
 Init:          false
Resources:
 Reservations:
  CPU:          0.25
  Memory:       128MiB
 Limits:
  CPU:          0.5
  Memory:       512MiB
Networks: shop_backend
Endpoint Mode:  vip
Ports:
 PublishedPort = 8000
  Protocol = tcp
  TargetPort = 8000
  PublishMode = ingress

(sample output, trimmed)

Things worth reading every time:

  • Image includes the @sha256: digest. When you deploy a tag, the manager resolves it to a digest so that every node runs exactly the same image, even if someone pushes a new image with the same tag later.
  • UpdateStatus tells you whether the last update completed, is updating, was paused or rollback_completed. It appears only after the first update.
  • Labels added by docker stack deploy show which stack the service belongs to (com.docker.stack.namespace).

Without --pretty, inspect prints the full JSON. Its main parts are Spec (what you asked for), PreviousSpec (the spec before the last update, used by rollback), Endpoint (virtual IPs and ports) and UpdateStatus.

Units in the spec

The API stores resources as integers: CPUs as NanoCPUs (1 CPU = 1,000,000,000) and memory in bytes. You saw the byte conversion in docker stack config:

compose · try it yourself
# $ docker stack config -c stack.yaml
# --- stack.yaml
services:
  api:
    image: ghcr.io/acme/shop-api:2.5.0
    deploy:
      resources:
        reservations:
          cpus: "0.25"
          memory: 128M
        limits:
          cpus: "0.5"
          memory: 0.5G
version: "3.13"
services:
  api:
    deploy:
      resources:
        limits:
          cpus: "0.5"
          memory: "536870912"
        reservations:
          cpus: "0.25"
          memory: "134217728"
    image: ghcr.io/acme/shop-api:2.5.0

cpus stays a string at this stage; it becomes NanoCPUs when the stack is deployed. A small converter makes sense of what docker service inspect prints:

python · try it yourself
import json

spec_json = """{
  "Spec": {
    "Name": "shop_api",
    "Mode": {"Replicated": {"Replicas": 3}},
    "TaskTemplate": {
      "ContainerSpec": {"Image": "ghcr.io/acme/shop-api:2.5.0@sha256:4b9d3c"},
      "Resources": {
        "Limits": {"NanoCPUs": 500000000, "MemoryBytes": 536870912},
        "Reservations": {"NanoCPUs": 250000000, "MemoryBytes": 134217728}
      }
    }
  },
  "PreviousSpec": {"TaskTemplate": {"ContainerSpec": {"Image": "ghcr.io/acme/shop-api:2.4.1@sha256:91af07"}}},
  "UpdateStatus": {"State": "completed", "Message": "update completed"}
}"""

svc = json.loads(spec_json)

def get(obj, path):
    """Follow a dotted path like a Go template {{.Spec.Name}}."""
    for key in path.strip(".").split("."):
        obj = obj[key]
    return obj

def human(res):
    return f"{res['NanoCPUs'] / 1e9:g} CPU, {res['MemoryBytes'] // 2**20} MiB"

print("name:      ", get(svc, ".Spec.Name"))
print("replicas:  ", get(svc, ".Spec.Mode.Replicated.Replicas"))
image = get(svc, ".Spec.TaskTemplate.ContainerSpec.Image")
print("image:     ", image.split("@")[0], "pinned to", image.split("@")[1])
print("previous:  ", get(svc, ".PreviousSpec.TaskTemplate.ContainerSpec.Image").split("@")[0])
print("limits:    ", human(get(svc, ".Spec.TaskTemplate.Resources.Limits")))
print("reserved:  ", human(get(svc, ".Spec.TaskTemplate.Resources.Reservations")))
print("update:    ", get(svc, ".UpdateStatus.State"))
name:       shop_api
replicas:   3
image:      ghcr.io/acme/shop-api:2.5.0 pinned to sha256:4b9d3c
previous:   ghcr.io/acme/shop-api:2.4.1
limits:     0.5 CPU, 512 MiB
reserved:   0.25 CPU, 128 MiB
update:     completed

--format queries

For scripts, ask for exactly one value with a Go template instead of parsing the whole JSON:

bash
$ docker service inspect -f '{{.Spec.Mode.Replicated.Replicas}}' shop_api
3
$ docker service inspect -f '{{.Spec.TaskTemplate.ContainerSpec.Image}}' shop_api
ghcr.io/acme/shop-api:2.5.0@sha256:4b9d3c...
$ docker service inspect -f '{{json .Spec.TaskTemplate.Resources.Limits}}' shop_api
{"NanoCPUs":500000000,"MemoryBytes":536870912}
$ docker service inspect -f '{{if .UpdateStatus}}{{.UpdateStatus.State}}{{else}}never updated{{end}}' shop_api
completed
$ docker service ls --format '{{.Name}} {{.Replicas}}'
shop_api 3/3
shop_web 2/2

(sample output)

The if guard matters: UpdateStatus is missing on a service that was never updated, and a template that reads a field of a missing object fails.

docker service ps

service ps lists a service's tasks, including recent history:

bash
$ docker service ps --format '{{.Name}} {{.Node}} {{.CurrentState}} {{.Error}}' shop_api
shop_api.1 worker-1 Running 9 minutes ago
shop_api.2 worker-3 Running 9 minutes ago
shop_api.2 worker-2 Shutdown 10 minutes ago
shop_api.3 worker-2 Pending 2 minutes ago no suitable node (insufficient resources on 3 nodes)

(sample output)

Option Use
--no-trunc full error messages (they are cut at about 30 characters otherwise)
--filter desired-state=running hide history; only tasks that should be running
--filter node=worker-2 tasks on one node
-q task IDs only, for docker inspect <task-id>

The ERROR column is where the scheduler and the nodes explain themselves: no suitable node (…) for placement problems, task: non-zero exit (1) for crashes, No such image: … for pull failures, starting container failed: … for runtime errors. --no-trunc is almost always worth it. docker inspect <task-id> gives the task's full status, including the container ID on its node.

docker service logs

bash
$ docker service logs --tail 20 --timestamps shop_api
shop_api.1.q8w7e6r5t4y3@worker-1    | 2026-10-18T09:12:44.118Z INFO  GET /health 200
shop_api.2.m1n2b3v4c5x6@worker-3    | 2026-10-18T09:12:44.731Z INFO  GET /products 200
$ docker service logs -f --raw shop_api           # follow, without the task prefix
$ docker service logs q8w7e6r5t4y3                # one task only

(sample output)

Logs are fetched from every node that runs a task and merged, each line prefixed with <task>@<node>. This needs a logging driver that supports reading back (json-file, local, journald). With a driver that ships logs elsewhere (syslog, gelf, awslogs), read them in that system instead (Logging & Monitoring).

docker node ps

The other direction: which tasks run on a node?

bash
$ docker node ps worker-2
ID             NAME          IMAGE                         NODE       DESIRED STATE   CURRENT STATE
m1n2b3v4c5x6   shop_api.3    ghcr.io/acme/shop-api:2.5.0   worker-2   Running         Running 9 minutes ago
f6g7h8j9k0l1   shop_web.1    nginx:1.27-alpine             worker-2   Running         Running 1 hour ago

(sample output)

Use it before draining a node, and on a node that seems overloaded.

A short checklist

  1. docker service ls: is REPLICAS complete? Is the image what you expect?
  2. docker service ps --no-trunc <svc>: errors on recent tasks? Pending tasks?
  3. docker service inspect --pretty <svc>: is the update finished or paused? Constraints and reservations as intended?
  4. docker service logs --tail 50 <svc>: what does the app say?
  5. On the node: docker ps, docker inspect and docker logs work as usual for a task's container (logs, exec & inspect).

Troubleshooting turns this into a full playbook.

Learn more: Rollbacks · Logging & Monitoring · kubectl describe and friends

Test yourself

Why does the image in docker service inspect end with @sha256:…?

  1. It is a typo in the CLI
  2. The manager resolved the tag to a digest so that every node runs exactly the same image
  3. Images in Swarm must be signed
  4. It is the container ID

A limit of 0.5 CPU appears in the service JSON as…

  1. "0.5"
  2. NanoCPUs: 500000000
  3. CPUShares: 512
  4. Millicores: 500

Where do you look first to find out why a task keeps failing or stays pending?

  1. docker service ls
  2. The ERROR column of docker service ps --no-trunc
  3. docker swarm join-token
  4. docker node promote