Akki
Akki

Reputation: 2359

How to get container id of a docker service

I want to get a container ID of docker service. Is there any command available for this ?

I tried

docker service ps MyService

but this one only gives the service ID, I am interested in the container id in which the service is running

Upvotes: 22

Views: 18286

Answers (5)

Manmohan
Manmohan

Reputation: 440

I tried this, it gives me a list of running services:

docker container ls

Upvotes: 1

Sachin G.
Sachin G.

Reputation: 1980

docker ps | grep "<service-name>\." | awk '{print $1}'

Upvotes: 1

Sri
Sri

Reputation: 4853

docker service ps -q -f desired-state=running SERVICE_NAME  | xargs docker inspect --format '{{.Status.ContainerStatus.ContainerID}}'

Upvotes: 6

Andriy
Andriy

Reputation: 15472

try combination of docker process filtering and formatting:

docker ps -f name=YOUR_SERVICE_NAME --format "{{.ID}}"

UPDATE

thanks to ahivert for even shorter solution:

# same behavior with
docker ps -f name=YOUR_SERVICE_NAME --quiet

Upvotes: 34

user2915097
user2915097

Reputation: 32216

try from

https://github.com/moby/moby/issues/31369

for f in $(docker service ps -q $service);do docker inspect --format '{{.NodeID}} {{.Status.ContainerStatus.ContainerID}}' $f; done

and

docker network inspect --verbose

from https://github.com/moby/moby/pull/31710

Upvotes: 13

Related Questions