4

I am newbie in docker swarm mode, If I want more details about my swarm service , from command line I can run docker service ps "service name ", or from docker rest "/v2/services/my-ngx"

[msreddy@swarmnode1 ~]$ docker service ps my-ngx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS voj7fllhnmqb my-ngx.1 nginx:latest swarmnode1 Running Running 26 minutes ago

but it didn't give me container id. I want track containers form my swarm service is this possible ?

thisisms
  • 371
  • 5
  • 19

3 Answers3

11

Id returned by docker service ps my-ngx is the <task_id> To get the container id, you can use docker inspect -f "{{.Status.ContainerStatus.ContainerID}}" <task_id>

prranay
  • 1,789
  • 5
  • 23
  • 33
  • 3
    It seems than with newer API the ContainerID is not available – guillem May 06 '17 at 20:48
  • On Mac, `Version 17.09.1-ce-mac42` still supports this command – ForgetfulFellow Feb 01 '18 at 19:52
  • The container id output from the command in the answer is 64 characters long. But `docker ps` outputs 12 character long IDs. How does the 64 character long ID correspond to the 12 character ID? – therobyouknow Dec 07 '20 at 23:11
  • +1 upvote on your answer thank you so much for this answer @prranay for this great command. To answer my own question, the 12 character ID we see in Container IDs in the output of the ordinary `docker ps` command is the first 12 characters of the 64 character ID. So it's a bit similar to git commit hashes which do something similar (if anyone wants a technical analogy). I've also written a related Question & Answer here that credits your command: https://devops.stackexchange.com/questions/12924/ (thanks again) – therobyouknow Dec 08 '20 at 01:20
5

The Solution of prranay work perfectly. To put it together with the question asked (bash):

for f in $(docker service ps -q my-ngx -f desired-state=running);do docker inspect --format '{{.Status.ContainerStatus.ContainerID}}' $f; done

This lists all containers of the mentioned service my-ngx

estani
  • 24,254
  • 2
  • 93
  • 76
0

I ended up here because I was trying to find the node with the given container ID. I knocked this up to do that:

#!/bin/sh
docker node ps $(docker node ls -q) -f desired-state=running | sort -u | while read -r taskid widge taskname image host rem ; do echo "$taskid   $taskname   $image   $host   `docker inspect -f \"{{.Status.ContainerStatus.ContainerID}}\" $taskid 2>/dev/null`"; done | grep $1

It's slow, but (without the grep) gives the container ID for every running task in the swarm.

Yaytay
  • 493
  • 4
  • 13