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

list all containers with restart policies

Is it possible to list all docker contianers with a restart policy set? I could inspect the running containers (How to quickly show policies of all docker containers), but what if one has been previously stopped, or has an on-failure policy and has completed successfully?

>Solution :

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

I could inspect the running containers (How to quickly show policies of all docker containers), but what if one has been previously stopped, or has an on-failure policy and has completed successfully?

Exactly, so do not stop at running containers – inspect all containers.

Just list all containers with that policy.

docker ps -a -q | xargs docker inspect | jq -r '.[] | select(.HostConfig.RestartPolicy.Name != "no") | .Name'

How does docker system prune work with this?

It does not work with this (?).

Will it skip the removal of any containers that are stopped, but have a restart policy set?

No, a stopped container is also removed. Observe the following terminal transcript:

$ docker run --restart always -d --name test alpine
7f22c1f1439b9a211ac85bf10b4da563aaa8f76a2229c290f1fceb0404ded2a8
kamil@leonidas /home/kamil
$ docker stop test
test
$ docker ps -a
CONTAINER ID   IMAGE          COMMAND        CREATED          STATUS                              PORTS     NAMES
7f22c1f1439b   alpine         "/bin/sh"      3 seconds ago    Exited (0) Less than a second ago             test
$ docker ps -a | grep test
7f22c1f1439b   alpine         "/bin/sh"      10 seconds ago   Exited (0) 7 seconds ago                test
$ yes y | docker system prune
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] Deleted Containers:
7f22c1f1439b9a211ac85bf10b4da563aaa8f76a2229c290f1fceb0404ded2a8

Total reclaimed space: 41B
$ docker ps -a | grep test
/* empty */
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