Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Last active August 10, 2020 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseluisq/246dd203b7bbfcd331c5229b2bd5e12c to your computer and use it in GitHub Desktop.
Save joseluisq/246dd203b7bbfcd331c5229b2bd5e12c to your computer and use it in GitHub Desktop.
How to get just the name of first Docker service running

How to get just the name of first Docker service running

Following command will get just the name of the first Docker service running (usually the last one deployed) of a list of N services with running/shutdown/etc status.

Format name: service_name.N.ID

docker service ps my_service_name \
    --no-trunc --format="{{.Name}}.{{.ID}}/{{.DesiredState}}" | grep "Running" | cut -f1 -d "/" | head -n1
# my_service_name.1.abcd1v23ui456pyu7lx8k9fze

More details at https://docs.docker.com/engine/reference/commandline/service_ps/#formatting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment