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?
CodePudding user response:
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 */