Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Programmatically stopping a docker container

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

docker stop <id>

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading