Home > OS >  Programmatically stopping a docker container
Programmatically stopping a docker container

Time:05-09

I want to programmatically fetch the id of a running container and stop it. However, I'm a little lost

Here's the command I use to fetch the id of the running container:

docker ps -q --no-trunc --format="{{.ID}}" --filter "ancestor=<repo-name>"

How do I pass the output of this command to

docker stop <id>

I'm new to docker, thus the question. TIA

CodePudding user response:

If you are using bash, you can use back ticks to evaluate a command and substitute in the command output, in your case:

docker stop `docker ps -q --no-trunc --format="{{.ID}}" --filter "ancestor=<repo-name>"`

Please, consider read this related Unix Stackexchange question, it may be of help as well.

As suggested in the linked question, you can use the $(...) syntax as well:

docker stop $(docker ps -q --no-trunc --format="{{.ID}}" --filter "ancestor=<repo-name>")

Although I don't know in this specific use case, the $(...) syntax has the further advantage of working with PowerShell too.

  • Related